【问题标题】:System.exit(0) vs JFrame.EXIT_ON_CLOSESystem.exit(0) 与 JFrame.EXIT_ON_CLOSE
【发布时间】:2011-12-21 22:16:38
【问题描述】:

这两者有什么区别。我正在阅读一篇关于您应该始终使用的文章 (http://www.javalobby.org/java/forums/t17933)

System.exit(0);

目前我使用

JFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

文章说,即使对于 Java Swing 应用程序,您也应该添加一个侦听器 WindowAdapter 并在其方法 windowClosing(WindowEvent e) 中调用 System.exit()

有什么不同吗?一种方法比另一种更好吗?

【问题讨论】:

    标签: java swing jframe exit


    【解决方案1】:

    如果您查看 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。

    【讨论】:

      【解决方案2】:

      好吧,考虑到 System.exit(0) 在 JFrame 编码中,两者都可以。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-15
        • 1970-01-01
        • 1970-01-01
        • 2012-11-01
        • 1970-01-01
        • 2016-01-27
        • 2015-05-19
        • 1970-01-01
        相关资源
        最近更新 更多