java - 我想通过重置按钮重置网格

标签 java swing grid-layout

public class ButtonGrid extends JFrame{


private final JButton[][] grid;
private int length,width;

public ButtonGrid(int width,int length){

    JPanel panel = new JPanel();
    JButton start = new JButton("Start");
    JButton reset = new JButton("Reset");
    panel.add(start);
    //start.addActionListener(this);
    panel.add(reset);

    add(panel,BorderLayout.PAGE_START);
    JPanel panel1 = new JPanel();
    panel1.setLayout(new GridLayout(width,length));
    grid = new JButton[width][length];

    for(int y=0; y<length; y++){
        for(int x=0; x<width; x++){
            grid[x][y]=new JButton();
            grid[x][y].setBackground(Color.WHITE);
            panel1.add(grid[x][y]);
            grid[x][y].addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    Object source = e.getSource();
                    JButton b1 = (JButton)source;
                    if(b1 == source){
                    b1.setBackground(Color.BLACK);
                    }
                }
            });
            reset.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    Object source = e.getSource();
                    JButton b1 = (JButton)source;
                    if(b1 == source){
                    b1.setBackground(Color.WHITE);
                }
                }
            });
            }
    }


    add(panel1,BorderLayout.CENTER);
    panel1.setBackground(Color.RED);

    setSize(600,600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
}
        public static void main(String[] args) {
     EventQueue.invokeLater(new Runnable() {
     public void run() {
     ButtonGrid bg = new ButtonGrid(25,25);
      }
     });
    }
}

在此代码中,我可以更改网格中按钮的背景,但我想通过再次将背景更改为白色来重置网格。我无法执行此操作,因为我的 actionListener 不允许使用 grid[x][y] 设置背景。请帮助我如何进行此操作。

最佳答案

I am not able to do this since my actionListener does not allow to set background using grid[x][y].

当然你可以做到这一点。 grid 变量是一个实例变量,因此您可以直接在 ActionListener 中访问它。因此,ActionListener 中的代码将是一个循环,迭代数组的两个维度,然后设置每个按钮的背景

for(int y=0; y<length; y++){
        for(int x=0; x<width; x++){
            grid[x][y].setBackground(Color.WHITE);
         }
}

此外,您不应该为每个按钮创建单独的 ActionListener。您可以使用以下代码共享监听器:

ActionListener black = new ActionListener()
{
    public void actionPerformed(ActionEvent e)
    {
        Object source = e.getSource();
        JButton b1 = (JButton)source;
        b1.setBackground(Color.BLACK);
    }
};

for(int y=0; y<length; y++){
        for(int x=0; x<width; x++){
            grid[x][y]=new JButton();
            grid[x][y].setBackground(Color.WHITE);
            panel1.add(grid[x][y]);
            grid[x][y].addActionListener(black);
         }
}

关于java - 我想通过重置按钮重置网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19523996/

相关文章:

java - 在 Swing 框架中动态添加和删除按钮

javascript - Bootstrap - 创建 100 列网格,与圆形结合

Java 网格布局和 JPanel

java - 在 GridLayout 上方添加 JLabel

java - 我必须解析 JSON 响应,我必须获取 leet talk 下的子节点(包含符号、字符、数字的随机字符串)

java - 如何将 gzipped rdf 文件加载到 rdf4j 存储库?

java - 安装 org.apache.commons.io?

java TimeZone getDefault 很慢?

java - 如何动态更改 JSplitPane 中的组件

Java 线程组织指南