简体   繁体   English

Java javax.swing.Timer-冻结我的GUI。 解?

[英]Java javax.swing.Timer - freezing my GUI. Solution?

I have a task running using javax.swing.Timer. 我有一个使用javax.swing.Timer运行的任务。 It executes once every few minutes. 每隔几分钟执行一次。 The task takes a second or two, and it is freezing my GUI. 该任务需要一两秒钟,并且冻结了我的GUI。

What can I do to fix this? 我该怎么做才能解决此问题?

Timer tasks are executed on the Event Dispatching thread so while your task is running, no other swing event can happen and your gui will freeze. 计时器任务是在事件调度线程上执行的,因此当您的任务运行时,不会发生其他任何摆动事件,并且您的GUI将冻结。 You should make a separate thread for such a long task or use SwingWorker. 对于如此长的任务,您应该创建一个单独的线程,或者使用SwingWorker。

See this question for more details. 有关更多详细信息,请参见此问题。

The task may be taking more that 2 seconds.. Does the task require access to GUI? 该任务可能需要2秒钟以上的时间。该任务是否需要访问GUI? If not use separate thread. 如果不使用单独的线程。 The specific part of the task which require GUI access, can use SwingWorker to update the GUI. 需要GUI访问的任务的特定部分可以使用SwingWorker来更新GUI。

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

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