为那个 JFrame 添加一个
WindowListener 并重载
public void windowClosing(WindowEvent e) {
  显示对话框("要退出吗?")
  if(是){
    System.exit(0);
  }else{
    return
  }
}

解决方案 »

  1.   

    import javax.swing.*;
    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);
      }
    }
      

  2.   

    不用重写windowClosing,继承JFrame,然后在构造函数里加入
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    作判断退出的做法如上做法
      

  3.   

    增加WindowListener监听。
    public void windowClosing(WindowEvent e)
    {
       dispose();
       System.exit(0);
    }
      

  4.   

    建议一般用beyond_xiruo(希偌) 得方法:
    this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      

  5.   

    两种都是可以实现的。
    如果你采用frm.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    则一定需要实现WindowListener接口,由windowClosing事件决定是否退出系统。