JButton 颜色不变

JButton color not changing

我正在尝试创建一个非常基本的 java GUI 程序来解决 Boggle board。
它使用递归 DFS。我想要发生的是按钮的背景颜色(我使用按钮只是因为它们已经是正方形)从黄色到红色,因为字母得到 "marked",当它们是 "unmarked."
如果我注释掉最后一行将它充电回黄色,以及使用 Thread.sleep() 延迟程序的方法,它按预期工作,将它们全部保留为红色,但如果我只取消注释延迟方法,它不会实时更新,当程序完成时所有背景都会变为红色 运行.
如果我取消注释将它变回黄色的行,它会一直保持黄色。

我不知道如何让按钮实时切换到红色和返回黄色。

if(r+1 != bBoard.length && c+1 != bBoard.length && !bBoard[r+1][c+1].equals(""))
    {
        String temp = bBoard[r][c];
        bBoard[r][c] = "";
        boardLabel[r][c].setBackground(Color.RED);
        if(dictionary.contains(word) && word.length() > 2)
        {
            wordList.add(word);
            delayProgram();

        }
        depthFirstSearch(bBoard, r+1, c+1, word);
        bBoard[r][c] = temp;
        boardLabel[r][c].setBackground(Color.YELLOW);
    }

默认情况下,您的应用程序启动单线程并在此线程上执行所有计算。当你改变组件的颜色时,只有当程序有空闲时间时才会重新绘制,但程序永远没有空闲时间,因为计算仍在继续。

您需要做的是将此循环中的代码放入另一个线程并从第二个线程更新 GUI。

这是一个可以帮助您入门的小示例。

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ColorfulButtons extends JFrame {

    private JLabel[] labels = new JLabel[5];

    // start the application, this starts the original thread
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ColorfulButtons frame = new ColorfulButtons();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public ColorfulButtons() {
        // create your gui
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        JPanel contentPane = new JPanel();
        setContentPane(contentPane);
        contentPane.setLayout(new GridLayout(1, 0, 0, 0));

        for (int i = 0; i < 5; i++) {
            JLabel lbl = new JLabel("TEXT");
            add(lbl);
            labels[i] = lbl;
        }

        // start the color changing thread
        new Thread(new Runnable() {

            public void run() {
                doTheThing();
            }
            // give it a name for debugging
        }, "DoTheThingThread").start();
    }

    private void doTheThing() {
        int index = 0;
        while (true) {
            // put label in final variable so I can use it inside anonymous classes
            final JLabel lbl = labels[index];

            // make label yellow on Event Dispatch Thread
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    lbl.setForeground(Color.YELLOW);
                }
            });

            // pause to give the gui time to redraw and process events 
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // make label red on Event Dispatch Thread
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    lbl.setForeground(Color.RED);
                }
            });

            // increment or reset index
            if (index < 5 - 1)
                index++;
            else
                index = 0;
        }
    }
}