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

发布于 2025-01-08 00:39:21 字数 1296 浏览 0 评论 0原文

我想在运行时使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白鸥掠海 2025-01-15 00:39:21
this.validate(); 
this.repaint(); 

并且不要使用 setBounds()。请改用布局管理器。例如GridBagLayout

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

And don't use setBounds(). Use LayoutManager instead. E.g. GridBagLayout

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文