简体   繁体   English

ActionListener和Thread.sleep

[英]ActionListener and Thread.sleep

I have a JButton, which, as soon as I click on it, I want to change some colors after a few seconds, for example from blue -> 1 sec -> red -> 1 sec -> yellow. 我有一个JButton,一旦我点击它,我想在几秒后改变一些颜色,例如从蓝色 - > 1秒 - >红色 - > 1秒 - >黄色。 My problem is that, as soon as I use the Thread.sleep function in ActionListener and I test my program, it freezes for the 2 seconds and gives me directly yellow. 我的问题是,只要我在ActionListener中使用Thread.sleep函数并测试我的程序,它会冻结2秒并直接给我黄色。 How can I fix that? 我该如何解决这个问题? Thanks a lot 非常感谢

ActionListener and Thread.sleep.. ActionListener和Thread.sleep ..

..don't play well together. ..不能一起玩。

Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. 不要阻止EDT(事件调度线程) - 当发生这种情况时,GUI将“冻结”。 Instead of calling Thread.sleep(n) implement a Swing Timer for repeating tasks or a SwingWorker for long running tasks. 而不是调用Thread.sleep(n)实现用于重复任务的Swing Timer或用于长时间运行的任务的SwingWorker See Concurrency in Swing for more details. 有关更多详细信息,请参阅Swing中的并发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM