Thread.sleep 和重绘 [英] Thread.sleep and repainting

查看:57
本文介绍了Thread.sleep 和重绘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a panel that displays text. I want the panel to change its text and then have the application pause before anything else happens. I'm using Thread.sleep(1000). For some reason, though, the application doesn't finish painting the panel before Thread.sleep gets called (the text doesn't get changed). I also tried this:

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

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

but no luck there either. Anyone have a suggestion? Thanks so much.

推荐答案

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

You are blocking the AWT Event Dispatch Thread (EDT). The EDT handle repainting and input events, so your code need not be multithreaded (which would be effectively impossible). Use javax.swing.Timer to send an event later on the EDT. (Do not confuse javax.swing.Timer with java.util.Timer!)

这篇关于Thread.sleep 和重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆