System.exit(0)vs JFrame.EXIT_ON_CLOSE
这两者之间有什么区别吗? 我正在阅读一篇关于你应该总是使用的文章( http://www.javalobby.org/java/forums/t17933 )
System.exit(0);
目前我用
JFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
文章说,即使对于Java Swing应用程序,您也应该添加一个侦听器WindowAdapter
,并在其方法windowClosing(WindowEvent e)
调用System.exit()
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,如果这是你想要它做的。
好吧,考虑到System.exit(0)在JFrame编码中,要么都可以。