简体   繁体   English

为多个Jbutton提供相同的actionListener

[英]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. 我已经编写了一个2x2x2的rubiks多维数据集求解器,我想让用户更好地输入他们的多维数据集,目前他们输入的数字已分配给多维数据集的颜色。 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. 例如0可以代表白色,1可以代表黄色,等等。我一直在研究一个GUI,它是由按钮组成的2d立方体,单击它们时会在一系列颜色上循环变化。 This is what I have so far but I can't get the actionListener to apply to all the buttons. 到目前为止,这是我所拥有的,但是我无法将actionListener应用于所有按钮。

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. 只需将ActionListener实例分配给变量,然后将其添加到循环中的JButtons中即可。

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. 您可以像下面这样创建一个ButtonListener类来存储索引。 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));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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