Question

I'm trying to make a game like bejeweled or candycrush for homework. 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. so i need two interconnected ActionListener. How can I do?

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



}
Was it helpful?

Solution

You could assign one instance of an ActionListener to all buttons, roughly like this:

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

Where the buttons are created:

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

    buttons[i].addActionListener(buttonActionListener);
}

But I'd strongly recommend to think about an appropriate structure for your game that is closer to the Model-View-Controller pattern: You should store your game state in a dedicated class, and not only in form of the icons that they show. Whatever you intended to achieve by setting the "actionCommand" of the buttons will most likely not work properly as long as you do not have an appropriate representation of the game state!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top