正确的设计,以prevent blackscreen问题的JFrame [英] Correct design to prevent blackscreen issue in JFrame
问题描述
在我的应用程序有一个主框架窗口 GUI
,即启动在执行人任务
服务。
提交的任务生成
在磁盘 A文件输出和存储。
只要O / P生成GUI(观察者)被告知邻/对产生的。
下面的问题是我保持等待循环主框架内,一旦收到通知,主面板粉刷主框架上。
有关小任务,这工作得很好,但作为线程任务的大小增加。直到计算完成的等待循环时间的增加和GUI主窗口变成黑色。
您可以帮我纠正了设计。还如何在这种情况下,的SwingWorker
线程的帮助。
直到计算完成后的等待时间循环的增加和GUI主窗口变成黑色。
块引用>然后就长时间运行的任务是在事件指派线程(EDT)的prevents重画本身的GUI执行。你需要在一个单独的线程来执行长时间运行的任务。一个是SwingWorker的一个单独的线程有一个API,您还可以根据需要在美国东部时间执行code,例如当code执行完毕或者当你有中间结果。
阅读从Swing教程中的部分在并发了解更多信息。您可以随时搜索论坛例如使用的SwingWorker的。
In my application I have a main frame window
GUI
, that launches a task in anexecutor
service.The submitted task
generates
output and storesin a file on Disk
.As soon as the o/p is generated GUI (observer) is informed of the o/p generated.
Here the problem is I am keeping a wait loop inside the main frame and as soon as a notification is received, the main panel is repainted on the main frame.
For small tasks this works fine, but as the size of the threaded task increases. The wait loop time increases and the GUI main window turns black till computations are done.
Can you please help me in correcting the design. Also How can a
SwingWorker
thread help in this case.解决方案The wait loop time increases and the GUI main window turns black till computations are done.
Then you long running task is executing on the Event Dispatch Thread (EDT) which prevents the GUI from repainting itself. You need to execute the long running task in a separate Thread. A SwingWorker is a separate Thread which has an API that also allows you to execute code on the EDT as required, for example when the code finishes executing or when you have intermediate results.
Read the section from the Swing tutorial on Concurrency for more information. You can always search the forums for example of using a SwingWorker.
这篇关于正确的设计,以prevent blackscreen问题的JFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!