0

我对 JDialog 有一些问题。我在 NetBeans 中创建了 Swing GUI。GUI 包含带有按钮、文本字段等的主框架。当我单击“下一步”按钮时,会显示 JDialog(在 NetBeans 中也创建,其他组件部分)。以下是此 JDialog 的自定义属性:

  • 模态=真;
  • 可调整大小 = 假;
  • 默认关闭操作 = DISPOSE

问题是,在 Windows XP(也可能是 7)上,当 JDialog 发生时,我无法通过最小化父表单来最小化应用程序。在 Ubuntu 上它按预期工作,但在 Windows 上却没有。

我查看了http://docs.oracle.com/javase/tutorial/uiswing/misc/modality.html ,这个例子也在 Ubuntu 和 Widows 上运行。因此,我尝试在演示中手动创建示例 JDialog。再次在 Ubuntu 上是 woking,在 Windows 上不是。

有人有什么想法吗?我还没有。

SAmple DEMO,适用于 Ubuntu,不适用于 Windows XP:

package javaapplication5;

public class TWs extends javax.swing.JFrame {

    /**
     * Creates new form TWs
     */
    public TWs() {
        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() {

        jDialog1 = new javax.swing.JDialog();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        jDialog1.setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
        jDialog1.setResizable(false);

        jLabel2.setText("Sample Dialog");

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jDialog1Layout.createSequentialGroup()
                .addContainerGap(212, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addGap(205, 205, 205))
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addGap(122, 122, 122)
                .addComponent(jLabel2)
                .addContainerGap(160, Short.MAX_VALUE))
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jButton1.setText("Show dialog");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(136, 136, 136)
                .addComponent(jButton1)
                .addContainerGap(164, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(57, 57, 57)
                .addComponent(jButton1)
                .addContainerGap(213, Short.MAX_VALUE))
        );

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

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        jDialog1.setSize(200,200);
        jDialog1.setVisible(true);
    }                                        

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

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TWs().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration                   
}
4

2 回答 2

0

请尝试以下评论

您只需使用此构造函数 JDialog(Parent, Title, model)

jDialog1 = new javax.swing.JDialog(this, "Test", false); 

并删除设置modalityType 的行。

jDialog1.setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);

我希望它会有所帮助。

(请注意,当您最小化 JFrame 时,子 JDialog 也会被最小化)

于 2014-05-05T10:04:35.113 回答
0

这里的问题是您对模态对话应该如何工作的期望,而不是 Swing 使用的实现。

不同操作系统上的描述行为是正确的。在 Windows 操作系统上,当窗体显示为模态时,它应该完全禁用父窗口及其子窗口,从而防止与它们的任何控件进行任何交互。您可以在记事本中打开的对话框和“您确定”退出对话框中看到此行为。

于 2017-01-23T03:22:59.883 回答