【问题标题】:JDialog.dispose() closes box but not the app. Clicking the corner cross closes bothJDialog.dispose() 关闭框但不关闭应用程序。单击角十字会关闭两者
【发布时间】:2021-04-06 11:04:36
【问题描述】:

NetBeans 12.3 / Java 15.0.2 / Ubuntu 16.04。

我在 NetBeans 中创建了一个 Maven/Java 应用程序,它只有一个 JDialog 和一个调用 dispose() 的按钮。在 NetBeans 中运行它。如果我用角落里的十字关闭它,对话框就会消失,应用程序会关闭/退出。当我用按钮关闭它时,对话框会消失,但应用程序永远不会完成。

对话框上有一个用于关闭的窗口侦听器(由 NetBeans 创建)。监听器调用System.exit(0)。使用调试我可以看到侦听器在一种情况下被执行,但在另一种情况下却没有。对话框默认关闭操作是dispose。

当然我可以只添加一个调用System.exit,但我的印象应该是 dispose 和用角叉关闭对话框应该没有区别?

public class NewJDialog extends javax.swing.JDialog {
    /**
     * Creates new form NewJDialog
     */
    public NewJDialog (java.awt.Frame parent, boolean modal) {
        super (parent, modal);
        initComponents ();
    }

    /**
     * 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() {

      button1 = new java.awt.Button();

      setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

      button1.setLabel("button1");
      button1.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            button1ActionPerformed(evt);
         }
      });

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(261, Short.MAX_VALUE)
            .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(73, 73, 73))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(174, Short.MAX_VALUE)
            .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(103, 103, 103))
      );

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

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
      dispose ();
    }                                       

    /**
     * @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 (NewJDialog.class.getName ()).log (java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger (NewJDialog.class.getName ()).log (java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger (NewJDialog.class.getName ()).log (java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger (NewJDialog.class.getName ()).log (java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater (new Runnable () {
            public void run () {
                NewJDialog dialog = new NewJDialog (new javax.swing.JFrame (), true);
                dialog.addWindowListener (new java.awt.event.WindowAdapter () {
                    @Override
                    public void windowClosing (java.awt.event.WindowEvent e) {
                        System.exit (0);
                    }
                });
                dialog.setVisible (true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private java.awt.Button button1;
    // End of variables declaration                   
}

【问题讨论】:

  • 文档是您的朋友。阅读what dispose() doeswhen windowClosing is called。 dispose() 只是破坏窗口,这就是为什么它是许多可能的默认关闭操作之一。 dispose() 不模拟按下系统提供的窗口控件中的关闭按钮。

标签: java netbeans jdialog


【解决方案1】:

将动作侦听器更改为 windowClosed 而不是 windowClosing 可以修复它(捕获两种类型的关闭)。正如 VGR 指出的那样,在文档中..

https://docs.oracle.com/javase/7/docs/api/java/awt/event/WindowListener.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-09
    • 2012-06-30
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    • 1970-01-01
    • 2022-12-08
    • 1970-01-01
    相关资源
    最近更新 更多