Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
270 views
in Technique[技术] by (71.8m points)

java - GUI freezes during for-loop once the button is clicked

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.

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

the GUI freezes once the user hits a JButton

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...