isGUIInitialized() 为 false,现在怎么办?
我一直在查看一些代码,发现人们在做
public static void main(String[] args) {
new ExampleCode();
}
ExampleCode () {
EventQueue.invokeLater(this);
}
public void run() {
if (EventQueueMonitor.isGUIInitialized()) {
guiInitialized();
} else {
EventQueueMonitor.addGUIInitializedListener(this);
}
}
这很有意义,但现在我的问题是他们如何保持代码运行。据我了解,代码转到 main--->ExampleCode--->Run 然后它停止,因为 GUI 未初始化。是否有任何调用在其他地方启动 GUI?我在我的程序上使用相同的步骤,但我的 GUI 未初始化。
我的两个示例代码:
I been looking at some code and I find people doing
public static void main(String[] args) {
new ExampleCode();
}
ExampleCode () {
EventQueue.invokeLater(this);
}
public void run() {
if (EventQueueMonitor.isGUIInitialized()) {
guiInitialized();
} else {
EventQueueMonitor.addGUIInitializedListener(this);
}
}
Which makes sense, but now my question is how they keep the code running. To my understanding the code goes to main--->ExampleCode--->Run and then it stops because GUI was not initialized. Does any of the calls start the GUI else where? I use the same steps on my program, but my GUI is not initialized.
Two of my example codes:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您发布的示例使用与辅助功能相关的功能,因此初始化可能需要更多时间。我们在使用 Swing 时遵循的做法是避免对事件队列进行大量初始化。原作者的逻辑是等待swing jframe等初始化完成,然后再初始化自己的组件。
实际的初始化逻辑写在 createGUI 方法中,该方法要么由 Swing 调用,要么由您自己的逻辑调用。您的程序将不会终止,因为 Swing 使用它自己的非守护线程(即,除非您调用 System.exit,否则您的 swing 程序将不会终止)。
The example you posted is using accessibility related features, hence it is possible that the initialization may take more time. The practice that we follow while using Swing is to avoid heavy initialization on event queue. What the original author's logic does is that he waits for swing jframe etc. to initialize completely, then he initialized his own components.
The actual initialization logic is written in createGUI method, which will either be called by Swing or by your own logic. You program will not terminate, since Swing uses its own non-daemon thread (i.e. unless you call System.exit, your swing program will not terminate).
下面是一个基于 Swing 教程中的示例的简单示例:
Here is a simple example based on examples from the Swing tutorial: