我的GUI被冻结了 [英] My GUI is frozen

查看:115
本文介绍了我的GUI被冻结了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些我无法理解的东西:我的Swing GUI包含一个播放和暂停按钮。我还有一个定义'ON'和'OFF'状态的静态变量。 (主程序生成GUI)。
通过克服'play',我将静态变量的状态更改为'ON',并在一个也修改GUI的线程中启动一个耗时的过程。只要静态变量是'ON'循环在同一个进程中。单击暂停会将静态变量更改为OFF。
但是通过点击播放,GUI就会冻结,因此:

I have something I can't understand: my Swing GUI contains a 'play' and 'pause' button. I have also a static variable that defines 'ON' and 'OFF' states. (The main program generates the GUI). By cliking on 'play' I change the state of my static variable to 'ON' and I launch a time-consuming process in a thread that also modifies the GUI. As long as the static variable is 'ON' loops in the same process. Clicking on 'pause' would change the static variable to OFF. But by clicking on 'play' the GUI is freezing and consequently:


  1. GUI不会更新

  2. 我的暂停按钮无法暂停此过程。

我听说过EDT和SwingWorker但我有一个简单的方法可以做到这一点。

I have heard about EDT and SwingWorker but I you have a simple way to do it I take it.

感谢您的帮助并原谅我糟糕的英语......

Thank you for your help and forgive my bad english...

推荐答案

问题是你正在负责更新GUI的同一个线程上进行密集,耗时的工作。 SwingWorker允许您将耗时的任务移动到单独的执行线程,从而使UI线程不受限制地执行其操作。

The problem is that you're doing the intensive, time-consuming work on the same thread responsible for updating the GUI. SwingWorker allows you to move time-consuming tasks to a separate thread of execution, thereby leaving the UI thread to do its thing uninhibited.

然而,它确实增加了一个复杂性:亲和力。在UI组件上调用方法通常需要您从UI线程执行此操作。因此,您需要使用特殊功能从工作线程返回UI线程。 SwingWorker也为你提供了这种能力。

However, it does add a further complication: affinity. Calling methods on UI components generally requires that you do so from the UI thread. Therefore, you need to use special functionality to get back to the UI thread from the worker thread. SwingWorker also gives you this ability.

我建议你阅读此文档

这篇关于我的GUI被冻结了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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