1

前言:这是我做的第一个真正的摇摆程序。

我有一个摇摆程序,其中一个 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)

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 :)

4

2 回答 2

6

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)

于 2010-06-25T00:28:32.263 回答
0

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
于 2010-06-25T00:23:04.160 回答