简体   繁体   中英

Giving mulitple Jbuttons the same actionListener

I have written a 2x2x2 rubiks cube solver and I want to make the experience for the user entering their cube better, currently they enter numbers which are assigned to colors of the cube. For example 0 could represent white, 1 could represent yellow etc. I have been working on a GUI that is a 2d cube made of buttons that when they are clicked change loop over an array of colors. This is what I have so far but I can't get the actionListener to apply to all the buttons.

public static void main(String[] args) {

    final int WINDOW_HEIGHT = 500;
    final int WINDOW_WIDTH = 700;

    //create a window
    window.setTitle("First Window");
    window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
    window.setResizable(false);


        allButtons();
}

private static void allButtons(){
    panel.setLayout(null);
    window.add(panel);


     final JButton button[]=new JButton[23];
        for(int i=0;i<button.length;i++){
            button[i] = new JButton();
        }

        panel.add(button[0]);
        button[0].setBounds(30, 30, 60, 60);
        final Color[] ColorArray = {Color.WHITE, Color.ORANGE,Color.GREEN,Color.RED,Color.BLUE,Color.YELLOW};

        button[0].addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                final int stickerNum = 24;

                if(stickerNum <= 3){
                    for(Color i : ColorArray){
                    button[0].setBackground(i);
                    cube[Side][0] = 0;
                    }
                }
            }
        });
}

Just assign the ActionListener instance to a variable and add that to JButtons in a loop.

public static void main(String[] args) {

    final int WINDOW_HEIGHT = 500;
    final int WINDOW_WIDTH = 700;

    //create a window
    window.setTitle("First Window");
    window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
    window.setResizable(false);

    allButtons();
}

private static void allButtons(){
    panel.setLayout(null);
    window.add(panel);

    final JButton button[]=new JButton[23];
    for(int i=0;i<button.length;i++){
        button[i] = new JButton();
    }

    panel.add(button[0]);
    button[0].setBounds(30, 30, 60, 60);
    final Color[] ColorArray = {Color.WHITE, Color.ORANGE,Color.GREEN,Color.RED,Color.BLUE,Color.YELLOW};

    ActionListener actionListener = new ActionListener(){

        public void actionPerformed(ActionEvent e){
            final int stickerNum = 24;

            if(stickerNum <= 3){
                for(Color i : ColorArray){
                    button[0].setBackground(i);
                    cube[Side][0] = 0;
                }
            }
        }
    };

    for(int i=0;i<button.length;i++){
        button[i].addActionListener( actionListener);
    }
}

You can just create an ButtonListener class like below which stores an index. Then you just add a new instance of the listener to every button.

public static void main(String[] args) {

    final int WINDOW_HEIGHT = 500;
    final int WINDOW_WIDTH = 700;

    //create a window
    window.setTitle("First Window");
    window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setVisible(true);
    window.setResizable(false);


    allButtons();
}

private static void allButtons() {
    panel.setLayout(null);
    window.add(panel);


    final JButton button[]=new JButton[23];
    for(int i=0;i<button.length;i++){
        button[i] = new JButton();
    }

    panel.add(button[0]);
    button[0].setBounds(30, 30, 60, 60);
    final Color[] ColorArray = {Color.WHITE, Color.ORANGE,Color.GREEN,Color.RED,Color.BLUE,Color.YELLOW};

    class ButtonListener implements ActionListener {
        private int buttonIndex;

        public ButtonListener(int buttonIndex) {
            this.buttonIndex = buttonIndex;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            final int stickerNum = 24;

            if(stickerNum <= 3){
                for(Color i : ColorArray){
                button[buttonIndex].setBackground(i);
                cube[Side][0] = 0;
                }
            }
        }
    }

    for(int i = 0; i < button.length; i++) {
        button[i].addActionListener(new ButtonListener(i));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM