加一个判断的函数,如下:
  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;
  }
  }

解决方案 »

  1.   

    addWindowListener(this);
    ...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
         ...
     }
      

  2.   

    不行啊,我也是用这种方法的,即然已经发生windowCloseing事件,即说明这个窗体将要关闭,有没有取消这个事件的操作?请大虾帮忙!
      

  3.   

    addWindowListener(this);
    ...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
         ...
     }应该行,我可以运行的起
      

  4.   

    import java.awt.*;
    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
           ...
      }
    }