Java - 仅在关闭时处理 JFrame?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4675122/
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
Java - Only dispose JFrame on close?
提问by Connor
In my Java program I would like that, regardless of any other windows being open, whenever the user clicks the red X in the corner, just thatswing frame closes. I experimented with JFrame.DISPOSE_ON_CLOSE and other window listeners, but when I exit one JFrame the system still exited.
在我的 Java 程序中,我希望无论任何其他窗口处于打开状态,只要用户单击角落中的红色 X,该摆动框架就会关闭。我尝试了 JFrame.DISPOSE_ON_CLOSE 和其他窗口侦听器,但是当我退出一个 JFrame 时,系统仍然退出。
To clarify, suppose I have two JFrames visible, exiting one automatically exits the other, and the system exits. Any ideas as to how to only close one JFrame? Thanks.
澄清一下,假设我有两个 JFrame 可见,退出一个会自动退出另一个,然后系统退出。关于如何只关闭一个 JFrame 的任何想法?谢谢。
回答by Robert
This works for me:
这对我有用:
public class Test {
public static class TestFrame implements Runnable{
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setSize(200, 300);
frame.setVisible(true);
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new TestFrame());
EventQueue.invokeLater(new TestFrame());
}
}
回答by Connor
Finnw was correct - I set a new WindowListener (although it never called System.exit() unless it does by default) but I can handle it moving forward now. Thank you all
Finnw 是正确的 - 我设置了一个新的 WindowListener(尽管它从不调用 System.exit(),除非它默认调用)但我现在可以处理它了。谢谢你们