java中的关闭窗口事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1984195/
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
close window event in java
提问by Guy
I added an window state listener as follow:
我添加了一个窗口状态侦听器,如下所示:
this.addWindowStateListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ExitAction.getInstance().actionPerformed(null);
}
});
But when I'm using the X close button the event is not called. I think it's something to do with netbean jdesktop framework. But I can't find what could be the problem. Thanks for your help.
但是当我使用 X 关闭按钮时,不会调用该事件。我认为这与 netbean jdesktop 框架有关。但我找不到可能是什么问题。谢谢你的帮助。
采纳答案by Hymanrabbit
windowClosing
is part of the WindowListener
interface. Use addWindowListener
instead of addWindowStateListener
.
windowClosing
是WindowListener
界面的一部分。使用addWindowListener
代替addWindowStateListener
。
回答by camickr
Normally you use a WindowListener for this.
通常,您为此使用 WindowListener。
Check out Closing an Applicationfor an approach I use, although I must admit I've never tried it with Netbeans since I don't use an IDE.
查看关闭应用程序以了解我使用的方法,尽管我必须承认我从未使用 Netbeans 尝试过它,因为我不使用 IDE。
回答by Drewen
As the Java documentation says, to actually close the window the listener should invoke the window's dispose or setVisible(false) method.
正如 Java 文档所说,要实际关闭窗口,侦听器应调用窗口的 dispose 或 setVisible(false) 方法。
回答by TofuBeer
As others have pointed out the WindowListener is what you are after... but you should do this from now on when overriding a method:
正如其他人指出的 WindowListener 是您所追求的......但是从现在开始,您应该在覆盖方法时执行此操作:
this.addWindowStateListener(
new WindowAdapter()
{
@Overrides
public void windowClosing(WindowEvent e)
{
ExitAction.getInstance().actionPerformed(null);
}
});
Then the compiler will tell you when you are not actually overriding a method and are instead adding a new method (that in this case will never be called).
然后编译器会告诉你什么时候你没有真正覆盖一个方法而是添加一个新方法(在这种情况下永远不会被调用)。
回答by Bozho
Not answering your question directly (since an answer has already been given), but I assume you want to quit your program (or just hide a window) on exit. There is a shorter solution for these situations:
不直接回答你的问题(因为已经给出了答案),但我假设你想在退出时退出你的程序(或只是隐藏一个窗口)。对于这些情况,有一个更短的解决方案:
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
回答by Guy
Thank you everyone for helping me solve the problem. I don't fully understend it but the following code solve the problem:
谢谢大家帮我解决问题。我不完全理解它,但以下代码解决了问题:
Frame[] frames = Frame.getFrames();
for(Frame f: frames){
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
ExitAction.getInstance().actionPerformed(null);
}
});
}
It look as if the framework add additional frames. Thanks,
看起来好像框架添加了额外的框架。谢谢,
回答by InfiniteRecursion
Just debugged a similar problem in my Swing program. It turned out to be a Java bug that kills the system UI events when ImageIO is called before the Swing UI is created. See here for a minimal example -
刚刚在我的 Swing 程序中调试了一个类似的问题。结果证明是一个 Java 错误,它在创建 Swing UI 之前调用 ImageIO 时会杀死系统 UI 事件。在这里看到一个最小的例子 -
This bug stops the system UI events, such as window-close, from being delivered to Java.
此错误会阻止系统 UI 事件(例如窗口关闭)传递给 Java。