【问题标题】:how do you stop an ActionListener from remembering previous events如何阻止 ActionListener 记住以前的事件
【发布时间】:2012-03-28 01:52:38
【问题描述】:

我对 java 还不够新。我正在尝试用 6 种不同的颜色填充 6x6 网格,而同一行或列中不会出现相同的颜色。在我的代码中,我设置了一个 6x6 的 JButton 网格,存储在一个名为按钮的数组中。当我按下其中一个 JButton 时,会设置一个 6x1 的 JButton 网格,称为paintBox。 paintBox 中的 JButton 在程序顶部声明为 fillRed、fillYellow 等。当我按下 fillRed 时,它会将 JButton 的后轮从 6x6 网格设置为红色,但是当我从 6x6 网格按下不同的 JButton 并尝试设置时to yellow 它将它设置为黄色,但也将设置为红色的原始 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();
                    }
                } 

            }
        };
        }
    }

【问题讨论】:

  • 我建议使用JColorChooser 而不是ColourGrid。如果由于某种原因颜色选择器不合适,请将ColourGrid 放入设置为模态trueJDialog 中。 (为了更好地衡量,将其重命名为 ColorGrid 只是为了与 J2SE 中使用的常用拼写保持一致。)

标签: java swing jbutton actionlistener


【解决方案1】:

您的问题是您的颜色按钮在 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 对象时,它都会获得新的 JButton,每个 JButton 只附加一个 ActionListener,并且只有最近的网格按钮的颜色发生变化。

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

【讨论】:

  • 感谢你们两位的建议,现在它工作正常。真的很感激帮助。
猜你喜欢
  • 1970-01-01
  • 2011-06-20
  • 2018-01-29
  • 2021-12-11
  • 1970-01-01
  • 2014-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多