java中JFrame 窗口关闭时,弹出一个对话框提示是否确定关闭

public class Test extends JFrame 
{
 
 public Test() {
  
  setSize(100, 200);
  setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
  
  addWindowListener(new WindowAdapter(){
   @Override
   public void windowClosing(WindowEvent e) {
     int   option=   JOptionPane.showConfirmDialog( 
                  Test.this, "确定退出系统? ", "提示 ",JOptionPane.YES_NO_CANCEL_OPTION); 
      if(option==JOptionPane.YES_OPTION) 
         if(e.getWindow()   ==   Test.this) 
         { 
         System.exit(0); 
         } 
         else 
         { 
         return; 
         
         } 
   }
  });
  
 }
 public static void main(String[] args) {
  new Test().setVisible(true);
 }
 
}
展开阅读全文

没有更多推荐了,返回首页