两者有什么区别。我正在阅读一篇关于您应该始终使用的文章 ( http://www.javalobby.org/java/forums/t17933)
System.exit(0);
目前我在用
JFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
文章说,即使对于 Java Swing 应用程序,您也应该添加一个监听器 WindowAdapter
并在其方法 windowClosing(WindowEvent e)
。
有区别吗?一种方法比另一种更好吗?
最佳答案
如果您查看 JFrame 代码,它会:
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
switch(defaultCloseOperation) {
...
case EXIT_ON_CLOSE:
// This needs to match the checkExit call in
// setDefaultCloseOperation
System.exit(0);
break;
}
}
}
所以,这完全是一回事。如果这是您想要的,我会设置 EXIT_ON_CLOSE。
关于java - System.exit(0) 与 JFrame.EXIT_ON_CLOSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8597140/