声明一个已经创建的JDialog

问题描述

这个问题可能没有任何意义,因为我非常困惑。我正在NetBeans中使用Swing创建UI。我已经使用Swing UI创建了一个对话框。当我单击主框架上的按钮时,我想打开此对话框。在源代码中,它已经将我的对话框声明为变量。当我尝试在此Dialog变量上使用setVisible时,它会显示框的顶部,标题退出按钮,没有显示添加到对话框中的按钮或文本。有人告诉我必须声明此对话框,但是自动生成代码尚未声明该对话框吗?

public class ComputeUI extends javax.swing.JFrame {

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

        GCDDialog = new javax.swing.jdialog();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jButton3 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        
        jLabel2 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        

        GCDDialog.setDefaultCloSEOperation(javax.swing.WindowConstants.disPOSE_ON_CLOSE);
        GCDDialog.setTitle("GCD");
        GCDDialog.setAlwaysOnTop(true);
        GCDDialog.setLocationByPlatform(true);
        GCDDialog.setModalityType(java.awt.Dialog.ModalityType.APPLICATION_MODAL);
        GCDDialog.setName("GCDDialog"); // NOI18N

        jLabel5.setText("Number:");

        jButton3.setText("Compute");

        

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(20,20,20)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2,javax.swing.GroupLayout.PREFERRED_SIZE,81,javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton3))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel9)
                        .addGap(18,18,18)
                        .addComponent(jLabel10)))
                .addContainerGap(171,Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22,22,22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField2,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25,25,25)
                .addComponent(jButton3)
                .addGap(33,33,33)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addContainerGap(71,Short.MAX_VALUE))
        );

        javax.swing.GroupLayout GCDDialogLayout = new javax.swing.GroupLayout(GCDDialog.getContentPane());
        GCDDialog.getContentPane().setLayout(GCDDialogLayout);
        GCDDialogLayout.setHorizontalGroup(
            GCDDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1,Short.MAX_VALUE)
        );
        GCDDialogLayout.setVerticalGroup(
            GCDDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(GCDDialogLayout.createSequentialGroup()
                .addComponent(jPanel1,javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0,Short.MAX_VALUE))
        );

        GCDDialog.getAccessibleContext().setAccessibleName("GCDDialog");



        setDefaultCloSEOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jMenu1.setText("File");

        jMenuItem2.setText("Exit");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem2);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Compute");

        jMenuItem1.setText("GCD");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        
        

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        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,279,Short.MAX_VALUE)
        );

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

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // Todo add your handling code here:
        System.exit(0);
    }                                          

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
        // Todo add your handling code here:
        GCDDialog.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(ComputeUI.class.getName()).log(java.util.logging.Level.SEVERE,null,ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ComputeUI.class.getName()).log(java.util.logging.Level.SEVERE,ex);
        } catch (illegalaccessexception ex) {
            java.util.logging.Logger.getLogger(ComputeUI.class.getName()).log(java.util.logging.Level.SEVERE,ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ComputeUI.class.getName()).log(java.util.logging.Level.SEVERE,ex);
        }
        //</editor-fold>

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

    // Variables declaration - do not modify                     
    
    private javax.swing.jdialog GCDDialog;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration                   
} 

解决方法

您需要在pack()之前在创建的JDialog GCDDialog上调用setVisible(true)。 (旁注:变量名称绝不能以大写字母开头)

您一次只能将Netbeans表单设计器用于一种表单。因此,您应该拥有ComputeUI.java和GCDDialog.java。这样,Netbeans将正确初始化这两种形式。

要在ComputeUI类中建立两个组件之间的链接,

  • 声明一个全局变量:GCDDialog myDialog;
  • 在构造函数中:myDialog = new GCDDialog(...); //如果是通过Netbeans表单设计器创建的,则将自动调用pack()
  • 以及在jMenuItem1ActionPerformed中:myDialog.setVisible(true);