我在运行时创建的 Swing 组件未显示在 JPanel 中
我想在运行时使用 JPanel 中的 for 循环创建一些 swing 组件,但它不显示任何内容。请帮我。
这是代码:
private String [] DTS = new String[]{"Number","Text","Date","Image","Document"};
private String [] CONS = new String[]{"Not Null", "Primary", "unique"};
private void GenerateButton_actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
System.out.println(TableName.getText());
System.out.println(ColumnRange.getText());
int Length = Integer.parseInt(ColumnRange.getText());
JTextField [] FieldName = new JTextField[Length];
JComboBox [] DataType = new JComboBox[Length];
JComboBox [] Constraints = new JComboBox[Length];
try {
for (int i=0; i < Length; i++) {
FieldName[i] = new JTextField();
FieldName[i].setBounds(new Rectangle(35,150,200,20));
DataType[i] = new JComboBox(DTS);
DataType[i].setBounds(new Rectangle(35,150,200,20));
Constraints[i] = new JComboBox(CONS);
Constraints[i].setBounds(new Rectangle(35,150,200,20));
this.add(FieldName[i],null);
this.add(DataType[i], null);
this.add(Constraints[i], null);
this.validate();
}
} catch(Exception GE){
System.out.println(GE);
}
}
I want to create some swing component at runtime using for loop in JPanel, while it doesn't display anything. please help me.
Here is the code :
private String [] DTS = new String[]{"Number","Text","Date","Image","Document"};
private String [] CONS = new String[]{"Not Null", "Primary", "unique"};
private void GenerateButton_actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
System.out.println(TableName.getText());
System.out.println(ColumnRange.getText());
int Length = Integer.parseInt(ColumnRange.getText());
JTextField [] FieldName = new JTextField[Length];
JComboBox [] DataType = new JComboBox[Length];
JComboBox [] Constraints = new JComboBox[Length];
try {
for (int i=0; i < Length; i++) {
FieldName[i] = new JTextField();
FieldName[i].setBounds(new Rectangle(35,150,200,20));
DataType[i] = new JComboBox(DTS);
DataType[i].setBounds(new Rectangle(35,150,200,20));
Constraints[i] = new JComboBox(CONS);
Constraints[i].setBounds(new Rectangle(35,150,200,20));
this.add(FieldName[i],null);
this.add(DataType[i], null);
this.add(Constraints[i], null);
this.validate();
}
} catch(Exception GE){
System.out.println(GE);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并且不要使用 setBounds()。请改用布局管理器。例如GridBagLayout
And don't use setBounds(). Use LayoutManager instead. E.g. GridBagLayout