Thread.sleep和重新绘制

rac*_*ach 2 java swing multithreading repaint

我有一个显示文本的面板.我希望面板更改其文本,然后在其他任何事情发生之前暂停应用程序.我正在使用Thread.sleep(1000).但是,出于某种原因,应用程序在调用Thread.sleep之前没有完成绘制面板(文本没有被更改).我也试过这个:

board.invalidate();
board.setLeftMessage("Not");
board.setRightMessage("Here");
board.revalidate();
Date current = new Date();
long timeNow = current.getTime();
Date newDate = new Date(timeNow + 1000);
while (current.before(newDate))
    current = new Date();
Run Code Online (Sandbox Code Playgroud)

但也没有运气.有人有建议吗?非常感谢.

Tom*_*ine 8

您正在阻止AWT事件调度线程(EDT).EDT处理重新绘制和输入事件,因此您的代码不需要多线程(这实际上是不可能的).用于javax.swing.Timer稍后在EDT上发送事件.(不要混淆javax.swing.Timerjava.util.Timer!)