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

时间:2012-02-23 12:25:42

标签: java swing runtime jdeveloper

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

1 个答案:

答案 0 :(得分:4)

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

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