编辑:下面是一个最简单、最简单且可检查的问题
RESUME
我正在做一个拉丁方应用程序,它设置一个大小为 s 的正方形,并且您需要使用一些限制来为其着色,例如同一行或同一列中的颜色不同。
但我的麻烦不是问题本身,而是Swing。
我正在尝试使用 Swing 来实现一些图形和更好的外观。
问题是,当我找到解决方案时,我想停下来几秒钟观察它,然后继续寻找其他解决方案(我将使用 Thread.sleep() 来完成此操作)。
但我发现正方形没有着色。只有当完成该方法时,它本身才会发生变化。
主要问题
仅显示它找到的最后一个解决方案,并在完成回溯方法时显示。
当我单击“解析”按钮时,有一个类实现了 ActionListener 接口,该接口具有 actionPerformed 方法,该类调用主框架类的方法来解析正方形。所以问题是,如果我在找到解决方案时停止执行,GUI 不会改变,
但在内部,当我检查属性(调试)时,单元格的颜色已更新,但在 GUI 中却没有。
我不知道为什么:(
更详细
我的想法是,制作一个带有两个面板的框架,一个在左边,一个在中间(也许将来,在右边放一些东西)。
为此,我使用了 BorderLayout。
左侧的第一个面板类似于基本配置菜单,用户可以在其中设置正方形的大小并运行它以获得解决方案。
为此,我有两个按钮,一个用于修改大小,另一个用于解决它。
所以我需要按钮事件。尺寸并没有给我带来问题,但可以解决。
所以我检查了一下,如果我给某个方块着色并暂停执行(Scanner.nextLine()或Thread.sleep())不会在GUI中进行更改,但是当我调试时,方块在属性中被着色,所以我不太明白为什么会失败。
我认为问题出在哪里
所以我有一个按钮,可以在单击时解析正方形。
我想,我真的不知道,但我怀疑,这样做会创建一个新的线程或其他东西,因此无法更新 GUI;并且只有在完成后才去做。
有什么办法可以改变这个吗?
class ResolveListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
int size = Integer.parseInt(textField.getText());
latinSquareFrame.resolve(size);
}
}
一个简单的问题
我已阅读评论来搜索与此类似的最小且易于检查的问题。
所以我做了。这段代码与我的问题类似,有一个正方形,我想在单击按钮时为其着色。
问题是,如果我暂停它,它不会着色,只有在方法完成时才会着色,我不知道为什么。
我认为这与我之前遇到的问题类似。
这是我能够用于解决我遇到的问题的最少代码。
package LatinSquare;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
TestFrame testFrame = new TestFrame();
}
}
class TestFrame extends JFrame
{
public TestFrame()
{
this.setVisible(true);
this.setBounds(400,300,400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
TestJPanel testJPanel = new TestJPanel();
this.add(testJPanel,BorderLayout.CENTER);
TestContainerButtonJPanel testContainerButtonJPanel = new TestContainerButtonJPanel(testJPanel);
this.add(testContainerButtonJPanel, BorderLayout.SOUTH);
this.revalidate();
this.repaint();
}
}
class TestContainerButtonJPanel extends JPanel
{
private JButton resolve;
public TestJPanel testJPanel;
public TestContainerButtonJPanel(TestJPanel testJPanel)
{
this.testJPanel = testJPanel;
this.setVisible(true);
resolve = new JButton("RESOLVE");
ActionListener resolveListener = new ResolveListener();
resolve.addActionListener(resolveListener);
this.add(resolve);
}
class ResolveListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
try {
TestContainerButtonJPanel.this.testJPanel.colourCells();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
class TestJPanel extends JPanel
{
private JButton[][] board;
public TestJPanel()
{
this.board = new JButton[4][4];
this.setVisible(true);
this.setLayout(new GridLayout(4,4));
for(int i=0; i<4;i++)
{
for(int j=0; j<4;j++)
{
JButton cell = new JButton();
board[i][j] = cell;
board[i][j].setBackground(Color.WHITE);
this.add(cell);
}
}
}
public void colourCells() throws InterruptedException {
for(int i=0; i<4;i++)
{
for(int j=0;j<4;j++)
{
this.board[i][j].setBackground(Color.RED);
Thread.sleep(300);
}
}
}
}