我希望在单击按钮时显示我的JDialog,但是当我单击它时,对话框确实显示,但是我在Dialog上看不到任何其他组件,因为我在对话框中添加了四个textped并添加了setVisible函数已经
class Class2_3 {
public static void main(String[]args){
MyFrame myFrame=new MyFrame();
myFrame.setVisible(true);
}
}
class MyDialog extends JDialog{
Container container =this.getContentPane();
JPanel jPanel=new JPanel();
public MyDialog(Component relativeTo)
{
super();
this.IniDialog();
this.setLocationRelativeTo(relativeTo);
this.setSize(300,300);
//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
setModal(true);
//this.setVisible(true);
}
public void IniDialog()
{
JTextField textField1=new JTextField(10);
textField1.setMaximumSize(textField1.getPreferredSize());
Box hBox1=Box.createHorizontalBox();
JTextField textField2=new JTextField(10);
textField2.setMaximumSize(textField2.getPreferredSize());
Box hBox2=Box.createHorizontalBox();
JTextField textField3=new JTextField(10);
textField3.setMaximumSize(textField3.getPreferredSize());
Box hBox3=Box.createHorizontalBox();
JTextField textField4=new JTextField(10);
textField4.setMaximumSize(textField4.getPreferredSize());
Box hBox4=Box.createHorizontalBox();
Box vBox=Box.createVerticalBox();
vBox.add(Box.createVerticalStrut(10));
vBox.add(hBox1);
vBox.add(Box.createVerticalStrut(10));
vBox.add(hBox2);
vBox.add(Box.createVerticalStrut(10));
vBox.add(hBox3);
vBox.add(Box.createVerticalStrut(10));
vBox.add(hBox4);
vBox.add(Box.createVerticalStrut(10));
jPanel.add(vBox);
container.add(jPanel);
}
}
class MyFrame extends JFrame implements ActionListener{
MyDialog dialog;
//Container contentPane=this.getContentPane();
public MyFrame()
{
//initialization
this.setTitle("Spike");
this.setSize(400, 300);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//Component adding
JButton openButton=new JButton("Open");
//openButton.setSize(50, 50);
openButton.addActionListener(this);
//contentPane.add(openButton);
JLabel label1=new JLabel("编号:");
JTextField textField1=new JTextField(10);
textField1.setMaximumSize(textField1.getPreferredSize());
Box hBox1=Box.createHorizontalBox();
hBox1.add(label1);
hBox1.add(textField1);
JLabel label2=new JLabel("名称:");
JTextField textField2=new JTextField(10);
textField2.setMaximumSize(textField2.getPreferredSize());
Box hBox2=Box.createHorizontalBox();
hBox2.add(label2);hBox2.add(textField2);
JLabel label3=new JLabel("单价:");
JTextField textField3=new JTextField(10);
textField3.setMaximumSize(textField3.getPreferredSize());
Box hBox3=Box.createHorizontalBox();
hBox3.add(label3);hBox3.add(textField3);
JLabel label4=new JLabel("数量:");
JTextField textField4=new JTextField(10);
textField4.setMaximumSize(textField4.getPreferredSize());
Box hBox4=Box.createHorizontalBox();
hBox4.add(label4);hBox4.add(textField4);
Box vBox=Box.createVerticalBox();
vBox.add(Box.createVerticalStrut(10));
vBox.add(hBox1);
vBox.add(Box.createVerticalStrut(10));
vBox.add(hBox2);
vBox.add(Box.createVerticalStrut(10));
vBox.add(hBox3);
vBox.add(Box.createVerticalStrut(10));
vBox.add(hBox4);
vBox.add(Box.createVerticalStrut(10));
vBox.add(openButton);
this.add(vBox,BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
if(dialog==null){
//dialog=(MyDialog) new JDialog(this,true);
dialog=new MyDialog(this);
dialog.setVisible(true);
}
}
}
答案 0 :(得分:3)
您忘记将文本字段添加到IniDialog()
:)
hBox1.add(textField1);
hBox2.add(textField2);
hBox3.add(textField3);
hBox4.add(textField4);