为什么我只能在 GUI 构造函数中更改按钮背景颜色?

Why can i only change button background color in the GUI constructor?

import java.awt.*;

import javax.swing.*;
import java.util.Timer;

public class GUI extends JPanel
{


    private static final long serialVersionUID = 1L;
    private static final int ROWS = 50;
    private static final int COLS = 30;
    public JButton[][] buttons = new JButton[ROWS][COLS];



    public GUI()
    {
        setLayout(new GridLayout(ROWS, COLS, 1,1));
        for (int row = 0; row < buttons.length; row++) {
             for (int col = 0; col < buttons[row].length; col++) {
                JButton button = new JButton("");
                add(button);
                buttons[row][col] = button;
                }
             }
              buttons[0][0].setBackground(Color.BLACK);



    }
    public void  start()
    {
            GUI gui = new GUI();

              JFrame frame = new JFrame("The Game Of Life");
              frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
              frame.getContentPane().add(gui);
              frame.pack();
              frame.setLocationByPlatform(true);
              frame.setVisible(true);  


    }
    public void run() 
    {
        this.start();       
        this.setColor('g', 0, 0);





    }
    public void setColor(char c, int i, int j)
    {
        setLayout(new GridLayout(ROWS, COLS, 1,1));

        for (int row = 0; row < buttons.length; row++) {
             for (int col = 0; col < buttons[row].length; col++) {
                JButton button = new JButton("");
                add(button);
                buttons[row][col] = button;

        switch(c){
        case 'g':
            buttons[i][j].setBackground(Color.GREEN);
        case 'r':
            buttons[i][j].setBackground(Color.RED);
        case 'b':
            buttons[i][j].setBackground(Color.BLACK);
             }
        }
        }
    }
}   

我可以让 "buttons[0][0]" 改变颜色,但其他地方似乎都不起作用(按钮 [0][0] 是一个例子,它是唯一可以改变按钮颜色的地方) class GUI 是应该创建生物学的系统的一部分 "game of life" 这是 Gui 界面的代码 我需要代码来创建 50x30 按钮的网格,我最终会制作每个按钮根据其周围其他按钮的属性改变颜色。此时最大的问题是我根本无法让按钮改变颜色,除非它在 ​​GUI 构造函数中完成。如果有人可以帮助我,我将非常感激这是 CS 项目的一部分。如果您认为自己需要更多信息,我很乐意 post 提供任何需要的信息,尽管提出来。 P.S。我意识到代码可能有点乱我一直在摆弄它所以我道歉。

import java.awt.*;
import javax.swing.*;


public class View extends JPanel
{
    private static final long serialVersionUID = 1L;
    private static final int ROWS = 50;
    private static final int COLS = 30;
    GUI gui = new GUI();

    JFrame frame;

    public View()
   {

}

public void run()
{

    //timer.start();
    gui.run();


}   

public void setButtons(Cell[][] colorset)
{
    for(int i = 0; i<ROWS; i++)
    {
        for(int j = 0; j<COLS; j++)
        {
            switch(colorset[i][j].getCurrent())
            {
            case 0:
                gui.setColor('g', i, j);
            case 1:
                gui.setColor('r', i, j);
            case 2:
                gui.setColor('b', i, j);
            default:

            }
        }
    }
    //this.run();
}

}'

您的问题是您希望在程序 运行 时调用 setColor(...)。解决办法是在一些事件代码中调用它,具体什么代码取决于你想触发什么事件的调用。如果是 JButton 按下,则将它放在 JButton 的 ActionListener 中。如果你想让它每 xxx 毫秒调用一次,那么将它放在 Swing Timer 的 ActionListener 中。如果您希望它响应按键,则将其放在键绑定操作中。