public App(){ //constructor
  ....
  setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
     int select=JOptionPane.showConfirmDialog(null,"exit");
     if(select==JOptionPane.YES_OPTION){
System.exit(0);
     } 
}
  });
 ...
}

解决方案 »

  1.   

    if(select==JOptionPane.YES_OPTION)
    {
        System.exit(0);

    这样就行了呀。如果点的是“取消”,那么返回值就不一样了,那不处理不就行了!
      

  2.   

    public void windowClosing(WindowEvent e)
       {
        int select=JOptionPane.showConfirmDialog(null,"真的要退出吗?");
        if(select==JOptionPane.YES_OPTION)
        {    }
        else
           //这里怎么写?
       }
    ---------------------------------------------------------------
       else 这里就不用多写了,因为setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
    这句就是你点了×以后,什么也不做。所以你在前面加上这句就可以了,
    setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);