import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class TestFrame {
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
int value=JOptionPane.showConfirmDialog(null, "确定要关闭吗?");
if (value==JOptionPane.OK_OPTION) {
System.exit(0);
}
}
});
}
}
给Frame添加一个窗口监听,在实现的windowClosing方法中调用dipose()
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
frame.dipose();
}
关闭JFrame用dispose()函数关闭 用System,exit(0)是关闭整个程序