单击按钮后,GUI在for循环期间冻结 [英] GUI freezes during for-loop once the button is clicked

查看:87
本文介绍了单击按钮后,GUI在for循环期间冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更详细地讲,我有一个循环,它接受数学运算,还有一个转换案例,在该转换案例中,它转换这些运算以便应用加法或减法.等等.

In more details, I have a loop that takes in mathematical operations and a switch case in which it translates those operations in order to apply addition or subtraction.. etc.

循环(int i)是它读取的操作数.条件也是输入的操作的一部分,也位于开关盒内.因此,无论何时用户输入该条件,以及循环应重复的数字(例如j),程序都应采用j并将其与i相等,但会冻结.我希望它分支到用户定义的正确条件的循环部分.

The loop (int i) is the number of operations it reads. The condition is also a part of the operations entered, it's also inside the switch case. So whenever the user enters that condition, and the number (for instance j) that the loop should repeat, the program should take in j and equalizes it with i but it freezes. I want it to branch to that part of the loop where the user defines to get the condition right.

一旦用户单击JButton,GUI就会冻结,以便要计算用TextArea编写的操作.

the GUI freezes once the user hits a JButton so that operations written in TextArea are to be calculated.

推荐答案

一旦用户点击JButton,GUI就会冻结

the GUI freezes once the user hits a JButton

在侦听器中调用的代码在事件调度线程(EDT)上执行.EDT负责处理事件和绘制GUI.因此,如果您在单击按钮时执行长时间运行的任务,则会导致GUI冻结,直到任务完成执行为止.

Code invoked in a listener executes on the Event Dispatch Thread (EDT). The EDT is responsible for handling events and painting the GUI. So if you execute long running tasks when you click on the button you will cause the GUI to freeze until the task finishes executing.

您需要在单独的线程上执行长时间运行的任务.一种方法是使用 Swing Worker .阅读有关并发的Swing教程中的部分,以获取有关以下内容的更多信息: EDT SwingWorker .

You need to execute the long running task on a separate Thread. One way to do this is to use a Swing Worker. Read the section from the Swing tutorial on Concurrency for more information about the EDT and SwingWorker.

这篇关于单击按钮后,GUI在for循环期间冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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