单击按钮更改图片 [英] Clicking the button to change the picture

查看:57
本文介绍了单击按钮更改图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作宝石迷或糖果迷之类的游戏.我创建了64个按钮单元8x8.我想更改单击的按钮的图像.但是我不能.如果您能帮助我,我会很高兴.还是我该怎么办?

I'm triyng to make a game like bejeweled or candy crush. I have created 64 buttons units 8x8. I want to change the image of buttons clicked. But i couldn't. I would be glad if you can help. Or how else can I do?

编辑:非常抱歉.我无法清楚地说明我的问题.我的问题,我想更改单击两个按钮的图片.例如,我单击了按钮[5],然后单击了按钮[11].那么button [5]的图标应为button [11]图标,而[11]的图标应为button [5]图标.

I'm so sorry. I couldn't explain my problem clearly. My problem, i want to change pictures of clicked two buttons. For example, i clicked buttons[5] and i clicked buttons[11]. Then icon of buttons[5] should be buttons[11] icon and icon of [11] should be buttons[5] icon.

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;
                case 4 : buttons[i]=new JButton(new ImageIcon("img//Taz.png"));
                        break;
                case 5 : buttons[i]=new JButton(new ImageIcon("img//Sam.png"));
                        break;
                case 6 : buttons[i]=new JButton(new ImageIcon("img//DuffyDuck.png"));
                        break;
                case 7 : buttons[i]=new JButton(new ImageIcon("img//Tweety.png"));
                        break;
                case 8 : buttons[i]=new JButton(new ImageIcon("img//Slyvester.png"));
                        break;
                case 9 : buttons[i]=new JButton(new ImageIcon("img//RoadRunner.png"));
                        break;
                }

                //Adding number to find easily
                String comand=Integer.toString(i);

                            //Get ImageIcon name
                final String imgName=((ImageIcon)buttons[i].getIcon()).toString();



                buttons[i].addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){




                        }
                    }
                });
                buttons[i].setActionCommand(comand);

                grid.add(buttons[i]);

          }



    add(grid);



}



public static void main(String[] args){
    butondeneme erdem=new butondeneme();
    erdem.setVisible(true);
}



   }

推荐答案

我认为您可以使用:

Button.setIcon(newImage);

这篇关于单击按钮更改图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆