这两者之间有什么区别吗?我在读一篇关于你应该经常使用的文章( http://www.javalobby.org/java/forums/t17933
System.exit(0);目前我使用的是
JFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );文章指出,即使对于Java Swing应用程序,也应该添加一个侦听器WindowAdapter,并在其windowClosing(WindowEvent e)方法中调用System.exit()。
有什么不同吗?哪种方法比另一种更好?
发布于 2011-12-22 06:19:05
如果你看一下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。
发布于 2013-11-12 09:38:25
好吧,考虑到System.exit(0)是在JFrame编码中,这两种方法都可以。
https://stackoverflow.com/questions/8597140
复制相似问题