[英]Declaring a JDialog that has already been created
This question might not make any sense, because I am incredibly confused.这个问题可能没有任何意义,因为我非常困惑。 I am creating a UI using Swing in Netbeans.我正在使用 Netbeans 中的 Swing 创建 UI。 I have created a Dialog box by using the Swing UI.我使用 Swing UI 创建了一个对话框。 I want to open this Dialog when I click on a button on the main Frame.我想在单击主框架上的按钮时打开此对话框。 In the Source Code it already declares my Dialog as a variable.在源代码中,它已经将我的 Dialog 声明为一个变量。 When I try to use setVisible on this Dialog variable it on shows the top of the box, the title and an exit button, none of the buttons or text that I added to the Dialog box.当我尝试在此 Dialog 变量上使用 setVisible 时,它会显示框的顶部、标题和退出按钮,但没有添加到对话框中的按钮或文本。 I was told I had to declare this Dialog box, but is it not already declared by the automatically generated code?有人告诉我必须声明这个对话框,但它不是已经被自动生成的代码声明了吗?
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.PREFERRED_SIZE, 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, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
GCDDialogLayout.setVerticalGroup(
GCDDialogLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(GCDDialogLayout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 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, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ComputeUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ComputeUI.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 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
}
You need to call pack()
on the created JDialog GCDDialog before setVisible(true)
.您需要在setVisible(true)
之前在创建的 JDialog GCDDialog 上调用pack()
setVisible(true)
。 (side note: variable names should never start with an uppercase letter) (旁注:变量名不应该以大写字母开头)
You should use the Netbeans form designer for only one form at a time.您应该一次仅将 Netbeans 表单设计器用于一种表单。 So you should have ComputeUI.java and GCDDialog.java.所以你应该有 ComputeUI.java 和 GCDDialog.java。 This way Netbeans will correctly initialize both forms.这样 Netbeans 将正确初始化两种形式。
To make the link between both components, in ComputeUI class:要在两个组件之间建立链接,在 ComputeUI 类中:
GCDDialog myDialog;
声明一个全局变量: GCDDialog myDialog;
myDialog = new GCDDialog(...);
在构造函数中: myDialog = new GCDDialog(...);
// if created via Netbeans form designer, pack() will be automatically called // 如果通过 Netbeans 表单设计器创建,会自动调用 pack()myDialog.setVisible(true);
并在 jMenuItem1ActionPerformed: myDialog.setVisible(true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.