为那个 JFrame 添加一个
WindowListener 并重载
public void windowClosing(WindowEvent e) {
显示对话框("要退出吗?")
if(是){
System.exit(0);
}else{
return
}
}
WindowListener 并重载
public void windowClosing(WindowEvent e) {
显示对话框("要退出吗?")
if(是){
System.exit(0);
}else{
return
}
}
import java.awt.event.*;public class Test{
public static void main(String[] args){
final JFrame frm = new JFrame();
frm.setSize(400,300);
frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
frm.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
int ret = JOptionPane.showConfirmDialog(frm, "Are you sure to exit?");
if(ret == JOptionPane.YES_OPTION){
System.exit(0);
}else{
return;
}
}
}
);
frm.setVisible(true);
}
}
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
作判断退出的做法如上做法
public void windowClosing(WindowEvent e)
{
dispose();
System.exit(0);
}
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
如果你采用frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
则一定需要实现WindowListener接口,由windowClosing事件决定是否退出系统。