加一个判断的函数,如下:
boolean giveup() {
if(!modified)
return true;
int temp=JOptionPane.showConfirmDialog(this,"Save changes?","Text Edit",JOptionPane.YES_NO_CANCEL_OPTION);
switch(temp) {
case JOptionPane.YES_OPTION:
return savefile();
case JOptionPane.NO_OPTION:
return true;
case JOptionPane.CANCEL_OPTION:
this.show();
return false;
default:
this.show();
return false;
}
}
boolean giveup() {
if(!modified)
return true;
int temp=JOptionPane.showConfirmDialog(this,"Save changes?","Text Edit",JOptionPane.YES_NO_CANCEL_OPTION);
switch(temp) {
case JOptionPane.YES_OPTION:
return savefile();
case JOptionPane.NO_OPTION:
return true;
case JOptionPane.CANCEL_OPTION:
this.show();
return false;
default:
this.show();
return false;
}
}
...public void windowClosing(WindowEvent e)
{
int opt = JOptionPane.showConfirmDialog(this,"Sure to quit?",""
,JOptionPane.YES_NO_OPTION);
if(opt == JOptionPane.YES_OPTION)
System.exit(0);
else
...
}
...public void windowClosing(WindowEvent e)
{
int opt = JOptionPane.showConfirmDialog(this,"Sure to quit?",""
,JOptionPane.YES_NO_OPTION);
if(opt == JOptionPane.YES_OPTION)
System.exit(0);
else
...
}应该行,我可以运行的起
import javax.swing.*;
import java.awt.event.*;public class Frame1 extends JFrame { public Frame1() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) {
Frame1 frame11 = new Frame1();
frame11.show();
} private void jbInit() throws Exception {
//注意关键在这里。
this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
this.addWindowListener(new java.awt.event.WindowAdapter() { public void windowClosing(WindowEvent e) {
this_windowClosing(e);
}
});
} void this_windowClosing(WindowEvent e) {
int intok = JOptionPane.showConfirmDialog(this, "", "", JOptionPane.OK_OPTION);
if(intok == 0)
//注意不要用System.exit(0),那样会让整个系统退出;
setVisible(false);
else
...
}
}