Swing Multithreading. My GUI is freezing

前端 未结 4 1596
孤城傲影
孤城傲影 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:14

    I think it is important that you understand that your program logic and GUI should never be run on the same thread at all. When your program is busy preforming tasks you do not want your GUI to freeze up. The way Java solves this is the Event Dispatch Thread (EDT). You do this like so:

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                //Your GUI code here
            }
        });
    }
    

    More info on the EDT here: https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html

提交回复
热议问题