JFrame关闭事件处理

1、JFrame实现WindowListener

2、将当前窗口加入监听

this.addWindowListener(this);
  //按关闭按钮,啥事也不做
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

3、实现windowClosing方法

/**
  * 关闭主窗口处理
  */
 @Override
 public void windowClosing(WindowEvent e) {
       int option = JOptionPane.showConfirmDialog(this, "确定退出系统?", "提示",
       JOptionPane.YES_NO_OPTION);
       if (option == JOptionPane.YES_OPTION)
       {
               if (e.getWindow() == this) {
                      this.dispose();
                      System.exit(0);
       } else {
              return;
       }
    }
   else if(option == JOptionPane.NO_OPTION){
          if (e.getWindow() == this) {
                   return;
          }
     }
 }

OK!

原文地址:https://www.cnblogs.com/zhishan/p/2627108.html