Java 动态添加gui组件?

Java 动态添加gui组件?,java,swing,user-interface,layout,jpanel,Java,Swing,User Interface,Layout,Jpanel,我正在开发一个程序,我想创建一个应用程序,在其中我可以使用for循环重复gui组件。我使用卡片布局完成了这项工作,它工作得很好,但是当我使用容器和JPanel而不使用卡片布局时,gui组件与前面的组件重叠。请给我一个提示或建议我的代码哪里错了。谢谢你的建议和提前的时间 以下是我的应用程序的代码: class form extends JFrame implements ActionListener { JTextArea text; static int openFrameC

我正在开发一个程序,我想创建一个应用程序,在其中我可以使用for循环重复gui组件。我使用卡片布局完成了这项工作,它工作得很好,但是当我使用容器和JPanel而不使用卡片布局时,gui组件与前面的组件重叠。请给我一个提示或建议我的代码哪里错了。谢谢你的建议和提前的时间

以下是我的应用程序的代码:

class form extends JFrame implements ActionListener {

    JTextArea text;
     static int openFrameCount = 0;
    public form(){
           super("Insert Form");
        Container panel=getContentPane();
        JPanel  cc    = new JPanel();
        cc.setLayout(null);
        for(int i=1;i<=2;i++){
        JLabel label1=new JLabel(" Question"+(++openFrameCount));

         label1.setBounds(15, 40, 185, 50);
        cc.add(label1);
        text=new JTextArea();
                text.setLineWrap(true);
        text.setWrapStyleWord(true);
        text.setPreferredSize(new Dimension(750,50));
        text.setBounds(80, 60,750,50);
        cc.add(text);
         JLabel symbol=new JLabel("Selection for Option?");
         symbol.setBounds(100, 120,850,60);
 cc.add(symbol);
   ButtonGroup group = new ButtonGroup();
  JRadioButton rbut=new JRadioButton("Radio Button for option");

        rbut.setBounds(300, 120,300,60);
         JCheckBox cbox=new JCheckBox("Check Box for option");
         cc.add(rbut);
         cbox.setBounds(650, 120,350,60);
         cc.add(cbox);
        group.add(rbut);
         group.add(cbox);

          cc.revalidate();
 validate();


         panel.add(cc);
        }
    }
类表单扩展JFrame实现ActionListener{
JTextArea文本;
静态int openFrameCount=0;
公共表格(){
超级(“插入表格”);
容器面板=getContentPane();
JPanel cc=新的JPanel();
cc.setLayout(空);

对于(int i=1;i您已经将
cc
面板的布局设置为
null
,这不是一个好主意。然后,使用
setBounds(x,y,width,height)
设置您添加的组件的位置和大小,它们当然会重叠


尝试使用任何适合您需要的布局管理器,但不要将其设置为null,除非您确实有非常充分的理由这样做。

感谢您的建议是的,通过将布局设置为流程布局以及卡片布局,它是有效的