[英]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? 运行运行JFrame(Swing)的程序时,为什么在线程0上运行时不显示窗口呢? Running on thread 0 can be done by (OS X): 可以通过(OS X)在线程0上运行:
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在EDT上对其进行排队来在EDT上运行代码:
SwingUtilities.invokeLater(() -> {
// start your GUI here
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.