在最后一个窗口上调用 dispose 后,java swing 程序未关闭

发布于 2024-09-07 03:23:23 字数 1043 浏览 2 评论 0原文

前言:这是我做的第一个真正的挥杆项目。

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不爱素颜 2024-09-14 03:23:23

因为 dispose() 方法仅释放资源。

该文档有一个

注意:当 Java 虚拟机 (VM) 内的最后一个可显示窗口被处理后,VM可能终止。请参阅AWT 线程问题 了解更多信息。

你注意到可能了吗?

上面的链接为您提供了有关自动关机功能的详细信息。您可以阅读有关此内容的更多信息,或者可以通过将 this.dispose() 替换为 System.exit(0) 来简单地解决此问题

Because dispose() method only releases the resources.

The doc has a

Note: When the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate. See AWT Threading Issues for more information.

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() with System.exit(0)

烦人精 2024-09-14 03:23:23

这篇 Pushing Pixels 文章:AWT 关闭和守护线程 讨论了 AWT 关闭行为1.4 中发生了变化。不过,文章指出,彻底关闭可能很棘手。

在看不到其余代码的情况下,我只能提供指针:

  • 确保没有其他尚未处理的隐藏帧
  • 确保AWT队列上没有生成消息(即在EventQueue中设置断点。)
  • 否则查看这些线程的堆栈帧并查看它们正在忙什么

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:

  • ensure there are no other hidden frames that have not been disposed
  • ensure no messages are being generated on the AWT queue (i.e. set a breakpoint in the EventQueue.)
  • otherwise look at the stack frame for these threads and see what they are busy doing
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文