在最后一个窗口上调用 dispose 后,java swing 程序未关闭
前言:这是我做的第一个真正的挥杆项目。
我有一个 swing 程序,其中一个 JButton 应该退出该程序。该按钮会触发 this.dispose();。当我单击此 JButton 时,它确实使窗口完全消失,但查看调试器,程序本身仍在运行。
我的主要方法仅包括:
public static void main (String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new StartupGui().setVisible(true);
}
});
}
我的退出按钮看起来像操作按钮看起来像:
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)
{
this.dispose();
}
我也尝试过这个退出按钮:
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
dispose();
}
});
}
在按下退出按钮后查看调试器,我看到以下内容(并且只有以下内容):
Daemon Thread [AWT-XAWT] (running)
Thread [AWT-Shutdown] (running)
Thread [AWT-EventQueue-0] (running)
Thread [DestroyJavaVM] (running)
可以有人指出我正确的方向,为什么程序在此之后没有关闭?我已经进行了一些谷歌搜索,但到目前为止还没有取得任何成果。如果您需要更多信息,请告诉我,
谢谢:)
Preface: This is the first real swing program I have done.
I have a swing program, where one JButton is supposed to exit the program. That button triggers this.dispose();. When i click this JButton, it does make the window completely go away, but looking at the debugger, the program itself is still running.
My main method only consists of:
public static void main (String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new StartupGui().setVisible(true);
}
});
}
My exit button looks like action button looks like:
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)
{
this.dispose();
}
I have also tried this for the exit button:
private void exitButtonActionPerformed(java.awt.event.ActionEvent evt)
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
dispose();
}
});
}
Looking at the debugger after i press the exit button, i see the following (and only the following):
Daemon Thread [AWT-XAWT] (running)
Thread [AWT-Shutdown] (running)
Thread [AWT-EventQueue-0] (running)
Thread [DestroyJavaVM] (running)
Can anyone point me in the right direction as to why the program isn't shutting down after this point? I have done some googling but haven't gotten anywhere thus far. If you need any more information, just let me know
Thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因为 dispose() 方法仅释放资源。
该文档有一个
你注意到可能了吗?
上面的链接为您提供了有关自动关机功能的详细信息。您可以阅读有关此内容的更多信息,或者可以通过将
this.dispose()
替换为System.exit(0)
来简单地解决此问题Because dispose() method only releases the resources.
The doc has a
Did you notice the may?
The link above gives you detailed information about the Auto shutdown feature. You can read more about that, or you can simply solve this by replacing
this.dispose()
withSystem.exit(0)
这篇 Pushing Pixels 文章:AWT 关闭和守护线程 讨论了 AWT 关闭行为1.4 中发生了变化。不过,文章指出,彻底关闭可能很棘手。
在看不到其余代码的情况下,我只能提供指针:
This Pushing Pixels article: AWT shutdown and daemon threads discusses the AWT shutdown behaviour that was changed in 1.4. Still, the article notes that it can be tricky to get a clean shutdown.
Without seeing the rest of the code, I can only offer pointers: