如何阻止ActionListener记住以前的事件

时间:2012-03-28 01:52:38

标签: java swing jbutton actionlistener

我对java很新。我想用6种不同的颜色填充6x6网格,而不会在同一行或列中出现相同的颜色。在我的代码中,我设置了一个存储在名为buttons的数组中的6x6 JButtons网格。当我按下其中一个JButton时,设置了一个名为paintBox的6x1 JButtons网格。 paintBox中的JButtons在程序的顶部声明为fillRed,fillYellow等。当我按下fillRed时,它将从6x6网格红色设置JButton的后轮,但是当我从6x6网格中按下不同的JButton并尝试设置黄色它将它设置为黄色,但也设置原来的JButton设置为红色到黄色。任何帮助都会很棒。 谢谢

import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class Grid4 extends JFrame implements ActionListener
    {
        private ColourGrid paintBox = null;
        private JButton fillRed = new JButton("Red");
        private JButton fillYellow = new JButton("Yellow");
        private JButton fillBlue = new JButton("Blue");
        private JButton fillGreen = new JButton("Green");
        private JButton fillPurple = new JButton("Purple");
        private JButton fillBrown = new JButton("Brown");
        private JButton[] paintButton = {fillRed,fillYellow,fillBlue,fillGreen,fillPurple,fillBrown};
        private Color[] colours = {Color.RED, Color.YELLOW, Color.BLUE, Color.GREEN, new Color(102, 0, 102), new Color(102, 51, 0)};
        public static void main(String[] args) // sets up a 6x6 grid
        {
            int rows = 6;
            int cols = 6;
            int size = 600;
            Grid4 grid = new Grid4(rows, cols);
            grid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            grid.setPreferredSize(new Dimension(size, size));
            grid.pack();
            grid.setLocationRelativeTo(null);
            grid.setVisible(true);
        }
        // main
        public Grid4(int rows, int cols) // makes the 6x6 main grid a grid of JButtons
        {
            int rowSize = 6;
            int colSize = 6;
            int gridSize = 600;
            JButton[][] buttons; //makes an array called buttons
            buttons = new JButton[rowSize][colSize];
            Container pane = getContentPane();
            pane.setLayout(new GridLayout(rows, cols));
            for(int j =0; j < rows; j++){
                for (int i = 0; i < cols; i++) {
                    buttons[j][i] = new JButton("");
                    buttons[j][i].setOpaque(true);
                    buttons[j][i].setName("");
                    buttons[j][i].addActionListener(this);
                    buttons[j][i].setBackground(Color.BLACK);
                    pane.add(buttons[j][i]);
                }
            }
        }               //end of grid constructor

        public void actionPerformed(ActionEvent e) 
        {
            if ( paintBox != null && paintBox.isShowing())//stops more than one paintBox from opening
                paintBox.dispose();
            if( e.getSource() instanceof JButton){// sets
                ((JButton)e.getSource()).setBackground(Color.BLACK);
            } 

            int rows = 6;
            int cols = 1;
            int size = 300;
            paintBox = new ColourGrid(rows, cols,(JButton)e.getSource());
            paintBox.setPreferredSize(new Dimension(size/3, size));
            paintBox.pack();
            paintBox.setVisible(true);
        }

        public class ColourGrid extends JFrame
        { 
            private JButton buttonPress;

            public ColourGrid(int rows, int cols, JButton button)
            {

                buttonPress = button;
                Container pane = getContentPane();
                pane.setLayout(new GridLayout(rows, cols));
                for (int i = 0; i < paintButton.length; i++) {
                    paintButton[i].setOpaque(true);
                    paintButton[i].addActionListener(buttonAction);
                    paintButton[i].setForeground(new Color(100,100,100));
                    paintButton[i].setBackground(colours[i]);
                    pane.add(paintButton[i]);
                }
            }
            private ActionListener buttonAction = new ActionListener()
            {
            public void actionPerformed(ActionEvent a)
            {
                if(a.getSource() instanceof JButton){
                    if((JButton)a.getSource()== fillRed){
                    buttonPress.setBackground(Color.RED);
                    dispose();
                    }
                    else if((JButton)a.getSource()== fillYellow){
                    buttonPress.setBackground(Color.YELLOW);
                    dispose();
                    }
                    else if((JButton)a.getSource()== fillBlue){
                    buttonPress.setBackground(Color.BLUE);
                    dispose();
                    }
                    else if((JButton)a.getSource()== fillGreen){
                    buttonPress.setBackground(Color.GREEN);
                    dispose();
                    }
                    else if((JButton)a.getSource()== fillPurple){
                    buttonPress.setBackground(new Color(102, 0, 102));
                    dispose();
                    }
                    else if((JButton)a.getSource()== fillBrown){
                    buttonPress.setBackground(new Color(102, 51, 0));
                    dispose();
                    }
                } 

            }
        };
        }
    }

1 个答案:

答案 0 :(得分:5)

您的问题是您的颜色按钮位于Grid4类中。每次创建新的ColourGrid对象时,都会将相同的颜色按钮添加到新的ColourGrid JFrame中,并将ActionListener重新添加到相同的按钮。因此,每次发生这种情况时,JButtons会累积另一个ActionListener,很快每当按下一个颜色按钮时,许多ActionListeners都会触发,无论是旧的还是新的,所有按钮都会改变颜色。

解决方案是让颜色按钮成为ColourGrid类的一部分,而不是Grid4类:

public class ColourGrid extends JFrame {
  private JButton fillRed = new JButton("Red");
  private JButton fillYellow = new JButton("Yellow");
  private JButton fillBlue = new JButton("Blue");
  private JButton fillGreen = new JButton("Green");
  private JButton fillPurple = new JButton("Purple");
  private JButton fillBrown = new JButton("Brown");
  private JButton[] paintButton = { fillRed, fillYellow, fillBlue, fillGreen,
        fillPurple, fillBrown };
  private Color[] colours = { Color.RED, Color.YELLOW, Color.BLUE,
        Color.GREEN, new Color(102, 0, 102), new Color(102, 51, 0) };

  private JButton buttonPress;

这样,每次创建一个新的ColourGrid对象时,它都会获得新的JButtons,每个JButton只附加一个ActionListener,并且只有最新的网格按钮颜色发生变化。

否则,安德鲁给你的所有建议都是非常好的推荐。