gpt4 book ai didi

java - 我在运行时创建的 Swing 组件未显示在 JPanel 中

转载 作者:行者123 更新时间:2023-11-29 03:49:30 27 4
gpt4 key购买 nike

我想在运行时使用 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);
}
}

最佳答案

this.validate(); 
this.repaint();

并且不要使用 setBounds()。请改用 LayoutManager。例如。 GridBagLayout

关于java - 我在运行时创建的 Swing 组件未显示在 JPanel 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9413032/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com