互连了两个ActionListener

时间:2014-03-02 12:48:38

标签: java swing jbutton actionlistener imageicon

我正在努力制作像宝石迷阵或candycrush这样的游戏作业。我想更改点击两个按钮的图片。例如,我点击了buttons[5],然后点击了buttons[11]。然后,buttons[5]的图标应为buttons[11]图标,[11]的图标应为buttons[5]图标。所以我需要两个相互关联的ActionListener。我该怎么办?

public class butondeneme extends JFrame{

private JPanel grid;
private JFrame jr;
public String x,y;

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];


        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;
                }


                String comand=Integer.toString(i);
                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);
}



}

1 个答案:

答案 0 :(得分:3)

您可以将ActionListener的一个实例分配给所有按钮,大致如下:

class ButtonActionListener extends ActionListener {
    private JButton previousButton = null;

    @Override
    public void actionPerformed(ActionEvent e){
        JButton currentButton = (JButton)e.getSource();
        if (previousButton == null) {
            previousButton = currentButton;
            return;
        }

        Icon previousIcon = previousButton.getIcon();
        Icon currentIcon = currentButton.getIcon();
        currentButton.setIcon(previousIcon);
        previousButton.setIcon(currentIcon);
        previousButton = null;
    }
}

创建按钮的位置:

ActionListener buttonActionListener = new ButtonActionListener();
for (int i = 0; i<buttons.length; i++) {
    ...
    buttons[i] = new JButton(...);

    buttons[i].addActionListener(buttonActionListener);
}

但我强烈建议您考虑一个更接近模型 - 视图 - 控制器模式的游戏的适当结构:您应该存储您的游戏状态在专门的类中,仅以它们显示的图标的形式。只要您没有适当的游戏状态表示,无论您打算通过设置按钮的“actionCommand”实现什么,都很可能无法正常工作!