简体   繁体   English

为什么我只能在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);
             }
        }
        }
    }
}   

I can get the "buttons[0][0]" to change color but nowhere else has seemed to work (the button[0][0] is there as an example that its the only place where it will change the button color) the class GUI is part of a system that is supposed to create the biology "game of life" this is the code for the Gui interface I need the code to create a grid of 50x30 buttons and I will end up making each button change color according to the attributes of the other buttons around it. 我可以获取“ buttons [0] [0]”来更改颜色,但是似乎没有其他地方起作用(button [0] [0]就是一个示例,它是唯一可以更改按钮颜色的地方)类GUI是应该创建生物学“生命游戏”的系统的一部分,这是Gui界面的代码,我需要代码来创建50x30按钮的网格,然后我将根据每个按钮改变颜色周围其他按钮的属性。 The biggest problem at this point is I cant get the buttons to change color at all unless its done in the GUI constructor. 此时最大的问题是,除非它在GUI构造函数中完成,否则我根本无法获得用于更改颜色的按钮。 if anybody can help me I would be incredibly thankful this is part of a CS project. 如果有人可以帮助我,我将非常感激,这是CS项目的一部分。 If you think you need more info I'm happy to post whatever is needed just ask. 如果您认为需要更多信息,我很乐意发布任何需要的信息,只是问一下。 PS I realize the code may be a little messy I have been fiddling with it so I apologize. 附言:我意识到代码可能有点混乱,我一直在摆弄它,所以对不起。

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();
}

}' }”

Your problem is that you want to have setColor(...) called while the program is running. 您的问题是您希望在程序运行时调用setColor(...) The solution is to call it in some event code, and what code will depend on what event you want to trigger its call. 解决方案是在某些事件代码中调用它,什么代码取决于您要触发其调用的事件。 If it's a JButton press, then place it inside of a JButton's ActionListener. 如果是按JButton,则将其放在JButton的ActionListener中。 If you want it called every xxx mSec's, then place it inside of the ActionListener of a Swing Timer. 如果您希望它每隔xxx个毫秒调用一次,则将其放在Swing计时器的ActionListener中。 If you want it to respond to key-press, then place it inside of a Key Bindings Action. 如果您希望它对按键做出响应,则将其放在“按键绑定动作”中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按下按钮时 Netbeans GUI 背景颜色更改 - Netbeans GUI Background Color Change on Button Press 为什么不能更改工具栏的背景颜色 - Why can't I change background color of toolbar 如何通过其构造函数中的String参数更改JPanel的背景颜色? - How can I change the background color of a JPanel through a String parameter in its constructor? 如何在Java GUI中设置按钮的背景颜色? - How to set background color of a button in Java GUI? java jTable CustomRenderer背景颜色仅在我单击它时才更改..为什么会发生这种情况 - java jTable CustomRenderer background color only change when i click it ..why this case happen 在第二个活动中单击一个按钮时,如何使用微调器更改主要活动的背景颜色? - How can I change the background color of a Main Activity by using spinner while clicking a button in the Second Activity? 如何使用单选按钮在GUI中更改形状的颜色 - How can I use radio buttons to change the color of a shape in a GUI 如何在图形GUI中更改线条的颜色 - How can i change the color of my lines in drawing GUI 如何更改 GUI 中标题边框的颜色? - How can I change the color of the titled border in my GUI? 如何更改javafx中listview中仅第一个单元格的背景颜色? - How can I change the background color of only the first cell in listview in javafx?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM