2012-02-23 81 views
1

我想在运行时使用for循环在JPanel中创建一些swing组件,但它不显示任何内容。请帮帮我。我在运行时创建的Swing组件不显示在JPanel

下面是代码:

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); 
    } 
} 
+1

我觉得这个问题每天至少要问50次.. – mre 2012-02-23 13:35:18

+1

请学习Java命名约定并使用它们。 – 2012-02-23 20:07:03

回答

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

,不使用的setBounds()。改为使用LayoutManager。例如。 GridBagLayout