Swing Multithreading. My GUI is freezing

前端 未结 4 1608
孤城傲影
孤城傲影 2021-01-28 05:41

Disclaimer: I\'m not using my program for anything malicious, even though it\'s name is Spambot. I\'m only using it for practice.

Edit: My problem is th

4条回答
  •  日久生厌
    2021-01-28 06:30

    What you should be using in any GUI application is not multithreading, but event-driven programming. That means never execute long-running loops inside an event handler, and never, ever, call Thread.sleep in one.

    Instead, a delayed GUI action must be scheduled on a Swing Timer, and the loop body must be put into the handler which you submit to the timer.

    If you don't have a delayed action, but truly a long-running task (that would mean you do a lot of computation or wait for I/O), only then would you need a background thread to do the work. In that case you would use the SwingWorker to communicate the task's results back to the GUI.

提交回复
热议问题