gpt4 book ai didi

Javax.swing : how to display updates to components properly?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:31:13 25 4
gpt4 key购买 nike

我正在编写一个带有图形显示的简单 Knight's Tour 程序。我已经将它编写为与控制台一起使用,现在我正在尝试将该代码转移过来,以便它与 swing 一起工作。我有一个按钮,其 Action Listener 启动并操作该过程。这是代码:

askButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

int[][] tour = getBoard(); // generates a matrix representing a successful knight's tour
int count = 0;

while (count < 64) {
count++;
Location loc = searchFor(count, tour); //gets the location of the int "count" inside the matrix "tour"
board.setVisible(false); //board contains a matrix of JPanels called "grid".
grid[loc.row()][loc.column()].add(new JLabel("" + count)); //adds JLabel to JPanel with the proper number
board.setVisible(true); //reset to visible to show changes
delay(1000); //wait 1000 milliseconds - one second - to allow user to view changes
}
}
});

这就是代码。我希望每个数字以一秒的间隔单独显示,但就目前而言,框架会暂时变为空白,并突然显示所有数字显示的结果,一旦完成。有人可以帮忙吗?谢谢。

最佳答案

把所有的答案放在一起

1.在从AWT / Swing Listener 执行的代码期间不要调用sleep(int)wait(int) , 使用

2.在add/remove/modify后需要调用已经可见的容器

  • revalidate()repaint(),然后是 setVisible()

3.setVisible 必须包裹在 invokeLater 中,按照您的本性编写

关于Javax.swing : how to display updates to components properly?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10775165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com