java JDialog为主窗口时正确关闭java程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7191330/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:00:44  来源:igfitidea点击:

Closing a java program properly when JDialog is the main window

javaswingwindowlistener

提问by jax

I have a JDialog as the main window in my application (originally it was a JFrame but it showed in the taskbar which I didn't want).

我的应用程序中有一个 JDialog 作为主窗口(最初它是一个 JFrame,但它显示在我不想要的任务栏中)。

Currently I am doing:

目前我正在做:

setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

and when I click an exit button:

当我点击退出按钮时:

frame.dispose();

But the process still seems to hang around in the background

但是这个过程似乎仍然在后台徘徊

JFrame had JFrame.EXIT_ON_CLOSEwhich seemed to do what I wanted.

JFrameJFrame.EXIT_ON_CLOSE似乎做了我想要的。

How can I close my application properly?

如何正确关闭我的应用程序?

采纳答案by Vincent Ramdhanie

You can add

你可以加

  System.exit(0);

where you want the program to end, maybe immediately after the dispose() line.

您希望程序结束的地方,可能是在 dispose() 行之后立即结束。

回答by Itay Maman

You need to add a WindowListener that will do System.exit(0) when the dialog closes.

您需要添加一个 WindowListener,它会在对话框关闭时执行 System.exit(0)。

JDialog dialog = ...;
dialog.addWindowListener(new WindowAdapter() { 
    @Override public void windowClosed(WindowEvent e) { 
      System.exit(0);
    }
  });

Of course, the System.exit(0) after you hit the Exit button (that was suggested elsewhere in this thread) is still needed.

当然,在您点击退出按钮(在本线程的其他地方建议)之后的 System.exit(0) 仍然需要。

回答by mKorbel

consider using JWindow(un-decoretad by defalut), but that's little bit complicating fact, that JWindowrequired initializations from JFrame(just must exist, nothing else) as parent

考虑使用JWindow(un-decoretad by defalut),但这有点复杂的事实,它JWindow需要从JFrame(只是必须存在,没有别的)作为父级进行初始化

better would be add WindowListenerand all Events/Actions would be redirected/managed this way

更好的是添加WindowListener所有事件/操作都将以这种方式重定向/管理

回答by mre

You know that the EXIT_ON_CLOSEfield is also inherited by JDialog, right?

您知道该EXIT_ON_CLOSE字段也由 继承JDialog,对吗?

As mentioned by @camickr, EXIT_ON_CLOSEis not a valid value for the setDefaultCloseOperationmethod of the JDialogclass. As stated by the API,

正如@camickr所提到的,EXIT_ON_CLOSE对于该类的setDefaultCloseOperation方法来说,它不是一个有效值JDialog。正如API所述,

Sets the operation that will happen by default when the user initiates a "close" on this dialog. You must specify one of the following choices:

  • DO_NOTHING_ON_CLOSE(defined in WindowConstants): Don't do anything; require the program to handle the operation in the windowClosing method of a registered WindowListener object.
  • HIDE_ON_CLOSE(defined in WindowConstants): Automatically hide the dialog after invoking any registered WindowListenerobjects.
  • DISPOSE_ON_CLOSE(defined in WindowConstants): Automatically hide and dispose the dialog after invoking any registered WindowListenerobjects.

设置当用户在此对话框上启动“关闭”时默认发生的操作。您必须指定以下选项之一:

  • DO_NOTHING_ON_CLOSE(定义于WindowConstants):不要做任何事情;要求程序在已注册的 WindowListener 对象的 windowClosing 方法中处理操作。
  • HIDE_ON_CLOSE(定义于WindowConstants):调用任何注册WindowListener对象后自动隐藏对话框。
  • DISPOSE_ON_CLOSE(在 WindowConstants 中定义):在调用任何注册WindowListener对象后自动隐藏和处置对话框。

If EXIT_ON_CLOSEis passed as an argument, an IllegalArgumentExceptionwill be thrown.

如果EXIT_ON_CLOSE作为参数传递,IllegalArgumentException则会抛出an 。

回答by Alessandro Mattiuzzi

For me worked only with windowClosing event:

对我来说,只使用 windowClosing 事件:

dialog.addWindowListener(new WindowAdapter() 
                @Override
                public void windowClosing(WindowEvent e) {
                     System.exit(0);
                }
    });

回答by ArifMustafa

you can try this below amazing source code -

您可以在以下惊人的源代码中尝试此操作-

JDialog dialog = (JDialog) container;
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setModal(false);
dialog.setVisible(false);
dialog.dispose();

Runtime.getRuntime().exit(1);

this above said will shut off the process as well as after disposing the JDialog container, also one more benefit is there, if this JDialog is running above any other JFrame or JDialog, so the parent will not terminate but if this JDialog is running on its own, then the process will get terminated completely, Enjoy.

上面说的将关闭进程以及在处理 JDialog 容器之后,还有一个好处是,如果此 JDialog 运行在任何其他 JFrame 或 JDialog 之上,则父级不会终止,但如果此 JDialog 正在其上运行自己的,那么该过程将完全终止,享受。