简体   繁体   中英

Running a JFrame in thread 0

When running a program that makes a JFrame (Swing), why is it that if it runs on thread 0 it does not show the window? Running on thread 0 can be done by (OS X):

java -XstartOnFirstThread Driver

Example

public class Driver
{
    public static void main (String args[])
    {   
        SwingUtilities.invokeLater(() -> { 
            WindowClass button = new WindowClass(450, 450);
        });
    }
}

public class WindowClass extends JFrame
{
    public WindowClass(int width, int height)
    {
        setTitle("Demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(width, height);
        setVisible(true);
    }
}

You need to show code, but you run code on the EDT by queuing it on the EDT using SwingUtilities:

SwingUtilities.invokeLater(() -> {
    // start your GUI here
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM