如何关闭JFrame应用程序?
关闭JFrame应用程序可以通过设置框架的默认关闭操作来完成。默认操作有一些已定义的常量。在所定义的这些常数javax.swing.WindowConstants接口包括EXIT_ON_CLOSE,HIDE_ON_CLOSE,DO_NOTHING_ON_CLOSE和DISPOSE_ON_CLOSE。
要退出应用程序,我们可以将其设置为EXIT_ON_CLOSE,当用户在框架上启动关闭操作时,此选项将调用System.exit()方法。
package org.nhooo.example.swing;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
public class MainFrameClose extends JFrame {
public static void main(String[] args) {
MainFrameClose frame = new MainFrameClose();
frame.setSize(250, 250);
// 定义JFrame的默认关闭操作为
// EXIT_ON_CLOSE该应用程序将通过调用退出
// 当用户在控制台上启动关闭事件时,System.exit()
// 帧。
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}