2013-08-05 69 views
1

我有一个棘手的问题。当用户点击窗口右上角的关闭标记时,我需要弹出一个确认消息框供用户决定是否关闭。但它不起作用。无论您在弹出消息框中选择什么,该对话框将始终关闭。但是当我创建一个JFrame时,同样的逻辑工作正常。如何监视JDialog的关闭事件?

下面是我的netbeans IED中的代码。 我只是用一个WindowAdpter来捕捉关闭事件,并且修改了由neatbean生成的虚拟代码。

谢谢。

import java.awt.event.WindowAdapter; 
import java.awt.event.WindowEvent; 
import javax.swing.JFrame; 
import javax.swing.JOptionPane; 


public class TestClosingDialog extends javax.swing.JDialog { 

    public TestClosingDialog(java.awt.Frame parent, boolean modal) { 
     super(parent, modal); 
     initComponents(); 


     this.addWindowListener(new WindowAdapter() { 
      @Override 
      public void windowClosing(WindowEvent e) { 
       int a = JOptionPane.showConfirmDialog(null,"Are you sure you need to close?", "Tip", JOptionPane.YES_NO_OPTION); 
       if (a == 0) { 
        System.out.println("yes " + a); 
        System.exit(0); //close 
       } else if (a==1) { 
        System.out.println("no " + a); 
       } 
      } 
     }); 
    } 

/** 
* This method is called from within the constructor to initialize the form. 
* WARNING: Do NOT modify this code. The content of this method is always 
* regenerated by the Form Editor. 
*/ 
@SuppressWarnings("unchecked") 
// <editor-fold defaultstate="collapsed" desc="Generated Code">       
private void initComponents() { 

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 
    getContentPane().setLayout(layout); 
    layout.setHorizontalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 400, Short.MAX_VALUE) 
    ); 
    layout.setVerticalGroup(
     layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
     .addGap(0, 300, Short.MAX_VALUE) 
    ); 

    pack(); 
}// </editor-fold>       

/** 
* @param args the command line arguments 
*/ 
public static void main(String args[]) { 
    /* Set the Nimbus look and feel */ 
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> 
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
    * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
    */ 
    try { 
     for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 
      if ("Nimbus".equals(info.getName())) { 
       javax.swing.UIManager.setLookAndFeel(info.getClassName()); 
       break; 
      } 
     } 
    } catch (ClassNotFoundException ex) { 
     java.util.logging.Logger.getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (InstantiationException ex) { 
     java.util.logging.Logger.getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (IllegalAccessException ex) { 
     java.util.logging.Logger.getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } catch (javax.swing.UnsupportedLookAndFeelException ex) { 
     java.util.logging.Logger.getLogger(TestClosingDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 
    } 
    //</editor-fold> 

    /* Create and display the dialog */ 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     @Override 
     public void run() { 
      TestClosingDialog dialog = new TestClosingDialog(new JFrame(), true); 
      dialog.setVisible(true); 
     } 
    }); 
} 
// Variables declaration - do not modify      
// End of variables declaration     
} 

回答

1

变化setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING);

这将需要在对话框中手动dispose。根据您的需求,您只需将需要改变System.exit(0);dispose();

我个人而言,建议不要直接从像JDialog顶层容器延伸,但是这只是我。

我更喜欢使用类似于JPanel的东西,它具有可显示对话框的辅助方法。这种方法会创建对话框并将其添加到它,例如

+0

非常感谢,哥们。有用。是的,我按照您的建议,使用JPanel以相同的方式执行相同的操作。这只是一个测试演示。真的很感激,你节省了我的时间。 – xuqin1019

+0

顺便说一句,你有关于如何打包和分发摆动桌面应用程序的任何想法。我已经在NetBeans中完成了一个swing项目,现在我需要打包并分发它。我使用mysql数据库。我可以将它作为一个exe安装文件打包吗?因此,即使用户的计算机上没有JRE环境,用户也可以像普通的Windows软件一样安装它。提前致谢。 – xuqin1019

+1

很明显,有很多种方式,从一个干净的构建开始。它会输出dist目录中的二进制文件。你可以创建一个安装程序,像[izPack](http://izpack.org/),它是开源的,可能适合初学者。你也可以看看[JNLP](http://docs.oracle.com/javase/tutorial/deployment/deploymentInDepth/jnlp.html)。创建'exe'文件并不是绝对的要求,我个人大部分时间都是这样做的,因为默认情况下我倾向于通过'7Zip'打开'jar'设置。我使用[exe4j](http://www.ej-technologies.com/products/exe4j/overview.html) – MadProgrammer