我只是对 java 新手而已。我正在尝试用 6 种不同的颜色填充 6x6 网格,而不是在同一行或列中出现相同的颜色。在我的代码中,我设置了一个 6x6 的 JButton 网格,存储在一个名为按钮的数组中。当我按下其中一个 JButton 时,会设置一个 6x1 的 JButton 网格,称为 paintBox。 paintBox 中的 JButtons 在程序顶部声明为 fillRed、fillYellow 等。当我按下 fillRed 时,它将 JButton 的后轮从 6x6 网格设置为红色,但是当我从 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();
}
}
}
};
}
}
最佳答案
您的问题是您的颜色按钮在 Grid4 类中。每次创建新的 ColourGrid 对象时,都会将完全相同的颜色按钮添加到新的 ColourGrid JFrame 中,然后将 ActionListener 重新添加到相同的按钮。因此,每次发生这种情况时,JButton 都会累积另一个 ActionListener,并且很快每当按下一个颜色按钮时,许多 ActionListener(新旧)都会触发,并且所有按钮都会改变颜色。
解决方案是让颜色按钮成为 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,每个 JButtons 只附加一个 ActionListener,并且只有最近的网格按钮的颜色发生变化。
否则,安德鲁给你的所有建议都是非常好的建议。
关于java - 你如何阻止 ActionListener 记住以前的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9900139/