简体   繁体   English

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

[英]Swing component that I created at runtime is not shown in JPanel

I want to create some swing component at runtime using for loop in JPanel, while it doesn't display anything. 我想在运行时使用JPanel中的for循环创建一些swing组件,而它不显示任何内容。 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);
    }
}
this.validate(); 
this.repaint(); 

And don't use setBounds(). 并且不要使用setBounds()。 Use LayoutManager instead. 请改用LayoutManager。 Eg GridBagLayout 例如GridBagLayout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM