正确的设计以防止

正确的设计以防止

本文介绍了正确的设计以防止 JFrame 中的黑屏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个主框架窗口 GUI,它在 executor 服务中启动一个任务.

In my application I have a main frame window GUI, that launches a task in an executor service.

提交的任务生成输出并将存储在磁盘上的文件中.

The submitted task generates output and stores in a file on Disk.

一旦生成 o/p,GUI(观察者)就会被告知生成的 o/p.

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.

对于小任务,这工作正常,但随着线程任务的大小增加.等待循环时间增加,GUI 主窗口变黑,直到计算完成.

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.

你能帮我更正设计吗?在这种情况下,SwingWorker 线程如何提供帮助.

Can you please help me in correcting the design. Also How can a SwingWorker thread help in this case.

推荐答案

然后您长时间运行的任务正在事件调度线程 (EDT) 上执行,这会阻止 GUI 重新绘制自身.您需要在单独的线程中执行长时间运行的任务.SwingWorker 是一个单独的线程,它有一个 API,它也允许您根据需要在 EDT 上执行代码,例如当代码完成执行或当您有中间结果时.

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.

阅读 Swing 教程中关于 并发 的部分更多信息.您可以随时搜索论坛,例如使用 SwingWorker.

Read the section from the Swing tutorial on Concurrency for more information. You can always search the forums for example of using a SwingWorker.

这篇关于正确的设计以防止 JFrame 中的黑屏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:08