我正在为我的项目创建一个gui.首次加载gui时,只能看到背景,因此按钮不可见,但当鼠标悬停在它们上面时,它们是可见的.有什么办法解决这个问题?
public class Home extends JFrame{
//New JPanel
private JPanel home;
//Creating image url. You must be change url
ImageIcon icon = new ImageIcon("img//home1.jpeg");
//Home Class
public Home(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 960, 640);
setTitle("LoneyTunes Crush");
home = new JPanel();
home.setBorder(new EmptyBorder(5, 5, 5, 5));
home.setLayout(new BorderLayout(0, 0));
setContentPane(home);
getContentPane().setLayout(null);
JLabel background = new JLabel(new ImageIcon("img//giphy."));
getContentPane().add(background);
background.setLayout(new FlowLayout());
//Creating Buttons
JButton play = new JButton("Play");
play.setBounds(20, 20, 200, 30);
JButton setting = new JButton("Settings");
setting.setBounds(20, 60, 200, 30);
JButton exit …Run Code Online (Sandbox Code Playgroud) 我喜欢做一个像宝石迷阵或糖果粉碎的游戏.我创建了64个按钮单元8x8.我想更改点击按钮的图像.但我不能.如果你能提供帮助,我会很高兴的.或者我怎么办?
编辑:我很抱歉.我无法清楚地解释我的问题.我的问题,我想改变点击两个按钮的图片.例如,我点击了按钮[5],然后点击了按钮[11].然后按钮[5]的图标应为按钮[11]图标,[11]的图标应为按钮[5]图标.
public class butondeneme extends JFrame{
private JPanel grid;
private JFrame jr;
public butondeneme(){
jr=new JFrame();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 640, 640);
grid=new JPanel();
grid.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
grid.setLayout(new GridLayout(8,8,5,5));
JButton[] buttons = new JButton[64];
//Creating random image for buttons
for (int i = 0; i<buttons.length; i++) {
Random r = new Random();
int a = r.nextInt(9)+1;
switch(a){
case 1 : buttons[i]=new JButton(new ImageIcon("img//Cakal.png"));
break;
case 2 : buttons[i]=new JButton(new ImageIcon("img//BugsBunny.png"));
break;
case 3 : buttons[i]=new JButton(new ImageIcon("img//Pig.png"));
break; …Run Code Online (Sandbox Code Playgroud)