给多个Jbuttons提供相同的actionListener

时间:2016-08-25 16:44:56

标签: java user-interface jbutton

我已经写了一个2x2x2 rubiks立方体解算器,我想让用户更好地输入他们的立方体体验,目前他们输入分配给立方体颜色的数字。例如0可以代表白色,1可以代表黄色等。我一直在研究一个由按钮组成的2D多维数据集,当它们被点击时会改变颜色数组的循环。这是我到目前为止所做的,但我无法将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;
                    }
                }
            }
        });
}

2 个答案:

答案 0 :(得分:1)

只需将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);
    }
}

答案 1 :(得分:0)

您可以创建一个类似于下面的ButtonListener类来存储索引。然后,您只需为每个按钮添加一个新的侦听器实例。

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