我有JDialog的代码:
public void createUI() {
contentPane2 = new JPanel();
contentPane2.setLayout(new FlowLayout());
contentPane2.setBackground(UIManager.getColor("menu"));
contentPane2.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane2);
contentPane2.setLayout(new FormLayout(new ColumnSpec[] {
ColumnSpec.decode("149px"),
ColumnSpec.decode("max(38dlu;default):grow"),
FormFactory.RELATED_GAP_COLSPEC,
ColumnSpec.decode("max(48dlu;default)"),
FormFactory.RELATED_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,
FormFactory.RELATED_GAP_COLSPEC,
ColumnSpec.decode("46px:grow"),
FormFactory.RELATED_GAP_COLSPEC,
FormFactory.DEFAULT_COLSPEC,},
new RowSpec[] {
FormFactory.RELATED_GAP_ROWSPEC,
RowSpec.decode("max(20dlu;default)"),
FormFactory.RELATED_GAP_ROWSPEC,
RowSpec.decode("14px"),
FormFactory.UNRELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,
FormFactory.RELATED_GAP_ROWSPEC,
FormFactory.DEFAULT_ROWSPEC,}));
JLabel lblAddNewTask = new JLabel("PLEASE FILL THE NEW TASK INFORMATION", SwingConstants.CENTER);
lblAddNewTask.setVerticalAlignment(SwingConstants.CENTER);
lblAddNewTask.setToolTipText("");
lblAddNewTask.setOpaque(true);
lblAddNewTask.setIgnoreRepaint(true);
lblAddNewTask.setFont(new Font("Verdana", Font.PLAIN, 11));
lblAddNewTask.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "", TitledBorder.LEADING, TitledBorder.TOP, null, Color.YELLOW));
lblAddNewTask.setBackground(new Color(255, 255, 255));
lblAddNewTask.setAlignmentX(0.5f);
contentPane2.add(lblAddNewTask, "1, 2, 8, 1, fill, fill");
JLabel lblChooseActivity = new JLabel("CHOOSE ACTIVITY :");
contentPane2.add(lblChooseActivity, "1, 6, center, default");
final JComboBox comboBox_1 = new JComboBox();
comboBox_1.addItem("Diagnosis");
comboBox_1.addItem("Treatment");
comboBox_1.addItem("Medication");
comboBox_1.addItem("Rehab");
comboBox_1.addItem("Discharge");
contentPane2.add(comboBox_1, "2, 6, left, default");
JLabel lblName = new JLabel("NEW TASK NAME :");
contentPane2.add(lblName, "1, 10, center, default");
textField = new JTextField();
contentPane2.add(textField, "2, 10, fill, default");
textField.setColumns(10);
JLabel lblDay = new JLabel("DAY :");
contentPane2.add(lblDay, "1, 14, center, default");
final JComboBox comboBox = new JComboBox();
comboBox.addItem("1");
comboBox.addItem("2");
comboBox.addItem("3");
contentPane2.add(comboBox, "2, 14, left, default");
final JCheckBox chckbxNewCheckBox = new JCheckBox("Complication");
contentPane2.add(chckbxNewCheckBox, "2, 18");
JButton btnAdd = new JButton("ADD");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println(comboBox_1.getSelectedItem()); //getActivity
//System.out.println(textField.getText()); //getTaskName
if (chckbxNewCheckBox.isSelected()){//if complication
System.out.println("*"+textField.getText());//setTaskName have * sign
JButton btn_6222 = new JButton("BAKAR BAKAR");
// btn_6222.addActionListener(deleteAction);
BigPanel.add(btn_6222, "4, 3");
BigPanel.updateUI();
}
else{
System.out.println(textField.getText());
}
System.out.println(comboBox.getSelectedItem()); //getDay
//System.out.println(chckbxNewCheckBox.getText());//getCheckedItem
String filename = "c:" + File.separator + "Text File from addCHECKUP Class.txt";
File f = new File(filename);
PrintWriter pw = null;
try {
pw = new PrintWriter(f);
pw.println(comboBox_1.getSelectedItem());
if (chckbxNewCheckBox.isSelected()){//if complication
pw.println("*"+textField.getText());
}
else{
pw.println(textField.getText());
}
pw.println(comboBox.getSelectedItem());
pw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally{
//no matter what happen. close the output stream always.
//note that closing a printer will not throw IOException
if(pw!=null){
pw.close();
}
}
JOptionPane.showMessageDialog(null,"Successfully added !");
}
});
contentPane2.add(btnAdd, "8, 20");
JDialog dialog = new JDialog();
dialog.setTitle("Add New Task");
dialog.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
dialog.setBounds(470, 180, 534, 323);
dialog.add(contentPane2);
dialog.setModal(true);
dialog.setVisible(true);
}
此代码用于执行按钮的操作。单击此按钮时,它将显示上面的JDialog createUI()
JButton button_3 = new JButton("ADD");//ADD ROW
button_3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
createUI();
}
});
问题是当我点击按钮时,JDialog没有显示出来。我试图弄清楚代码有什么问题却找不到它。请有人看看可能的解决方案。谢谢
答案 0 :(得分:0)
尝试致电
dialog.pack();
在将其设置为可见之前。