1

我有一个棘手的问题。当有人单击窗口右上角的关闭标志时,我需要弹出一个确认消息框供用户决定关闭与否。但它不起作用。无论您在弹出消息框中选择什么,对话框都将始终关闭。但是当我创建一个 JFrame 时,同样的逻辑可以正常工作。

下面是我在 netbeans IED 中的代码。我只是使用 WindowAdpter 来捕获关闭事件,请忽略由neabean 生成的虚拟代码。

谢谢。

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                   
 }
4

1 回答 1

1

更改setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING);

这将需要您手动dispose打开对话框。根据您的需要,您只需要更改System.exit(0);dispose();

我个人建议不要直接从顶级容器扩展JDialog,但那只是我。

我更喜欢使用类似的东西,JPanel它有一个可以显示对话框的辅助方法。例如,此方法将创建对话框并将面板添加到其中

于 2013-08-05T02:04:55.517 回答