Why do people run Java GUI's on the Event Queue

后端 未结 2 1672
遇见更好的自我
遇见更好的自我 2020-12-10 15:28

In Java, to create and show a new JFrame, I simply do this:

public static void main(String[] args) {
   new MyCustomFrameClass().setVisible(true         


        
2条回答
  •  误落风尘
    2020-12-10 15:46

    This line is modifying a Swing component since your custom frame is a subclass of JFrame:

    new MyCustomFrameClass().setVisible(true);
    

    Generally, you should never modify a Swing component unless you are on the Event Dispatch Thread (EDT).

    The following code will run whatever is in the Runnable on the EDT.

    EventQueue.invokeLater(Runnable);
    

    Now, the setVisible(true) call will be on the EDT as it should.

提交回复
热议问题