Java 动态添加文本字段时设置布局

Java 动态添加文本字段时设置布局,java,swing,jtable,Java,Swing,Jtable,我正在panel1中添加动态JTextField和JLabel,但我无法设置JTextField和JLabel的布局。我需要将JTextfield和JLabel添加到panel1中,并将panel1添加到panel。我需要以自上而下的方式添加JTextFields和JLabels,并设置布局panel1和panel是JPanel的实例 我的代码: public class MakeScrollablePanel extends JFrame implements ActionListener {

我正在
panel1
中添加动态
JTextField
JLabel
,但我无法设置
JTextField
JLabel
的布局。我需要将
JTextfield
JLabel
添加到
panel1
中,并将
panel1
添加到
panel
。我需要以自上而下的方式添加
JTextField
s和
JLabel
s,并设置布局
panel1
panel
是JPanel的实例

我的代码:

public class MakeScrollablePanel extends JFrame implements ActionListener
{

    static JButton jButton11,jButton12;
    static JPanel panel,panel1;
    static JTextField jTextFields;
    static JLabel label;
    static JComboBox<String> jComboBox;
    static Dimension dime,dime1,dime2,dime3,dime4,dime5;
    static JScrollPane scroll;
    private GridBagConstraints panelConstraints = new GridBagConstraints();  
    BoxLayout bx=null;//  @jve:decl-index=0:

    int count=1,i=0;

    public MakeScrollablePanel() 
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Show(); 

        add(jButton11);
        add(scroll);
        dime=new Dimension(600,550);
        setSize(dime);
        setTitle("Priyank Panel");
        setLayout(new FlowLayout());
        setLocationRelativeTo(null);
        setVisible(true);
        setResizable(true);

    }
     private void Show()
     {
         jButton11=new JButton("Add Designation");
         panel=new JPanel(); 
         bx=new BoxLayout(panel,BoxLayout.Y_AXIS);

         scroll=new JScrollPane(panel);
         dime1=new Dimension(500,3000);
         dime5=new Dimension(500,450);
         panelConstraints = new GridBagConstraints();
         scroll.setPreferredSize(dime5);
         scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
         panel.setLayout(bx);
         panel.add(Box.createHorizontalBox());
         panel.setBorder(LineBorder.createBlackLineBorder());
         panel.setBackground(new Color(204, 230 , 255));
        jButton11.addActionListener(this);

     }
     public void actionPerformed(ActionEvent event) 
        {
            if(event.getSource()==jButton11)
             {
                label=new JLabel("Add Designation "+count  +" :-");
                jTextFields=new JTextField(30);

                panel1=new JPanel();
                panel1.setBackground(new Color(204, 230 , 255));
                panel1.add(label);
                panel1.add(jTextFields); 
                    panel.add(panel1);
                    panel1.revalidate();
                                panel.revalidate();
                     panel.updateUI();
                count++;
                i++;
             }
        }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MakeScrollablePanel();

            }
        });

    }

}
公共类MakeScrollablePanel扩展JFrame实现ActionListener
{
静态JButton jButton11、jButton12;
静态JPanel面板,面板1;
静态JTextField jTextFields;
静态标签;
静态JComboBox-JComboBox;
静态尺寸dime,dime1,dime2,dime3,dime4,dime5;
静态JScrollPane滚动;
私有GridBagConstraints panelConstraints=新GridBagConstraints();
BoxLayout bx=null;/@jve:decl index=0:
整数计数=1,i=0;
公共MakeScrollablePanel()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Show();
添加(jButton11);
添加(滚动);
dime=新尺寸(600550);
设置大小(一角);
setTitle(“Priyank小组”);
setLayout(新的FlowLayout());
setLocationRelativeTo(空);
setVisible(真);
可设置大小(真);
}
私人电视节目()
{
jButton11=新JButton(“添加名称”);
panel=新的JPanel();
bx=新的BoxLayout(面板,BoxLayout.Y_轴);
滚动=新的JScrollPane(面板);
尺寸1=新尺寸(5003000);
尺寸5=新尺寸(500450);
panelConstraints=新的GridBagConstraints();
滚动。设置首选大小(dime5);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u根据需要);
面板设置布局(bx);
panel.add(Box.createHorizontalBox());
panel.setOrder(LineBorder.createBlackLineBorder());
面板.立根背景(新颜色(204230255));
jButton11.addActionListener(这个);
}
已执行的公共无效操作(操作事件)
{
if(event.getSource()==jButton11)
{
标签=新的JLabel(“添加名称”+计数+:-”;
jTextFields=新的JTextField(30);
panel1=新的JPanel();
镶板1.立根背景(新颜色(204230255));
面板1.添加(标签);
panel1.添加(jTextFields);
小组。添加(小组1);
面板1.重新验证();
panel.revalidate();
panel.updateUI();
计数++;
i++;
}
}
公共静态void main(字符串[]args)
{
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
新的MakeScrollablePanel();
}
});
}
}
仍不工作

但我仍然看不出有任何问题(事实上使用了BoxLayout而不是GridLayout,但在使用许多JTextFields的情况下,结果可能非常相似)

来自(少量)修改的OPs代码

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class MakeScrollablePanel extends JFrame implements ActionListener {

    private JButton jButton11, jButton12;
    private JPanel panel, panel1;
    private JTextField jTextFields;
    private JLabel label;
    private JComboBox<String> jComboBox;
    private Dimension dime, dime1, dime2, dime3, dime4, dime5;
    private JScrollPane scroll;
    private GridBagConstraints panelConstraints = new GridBagConstraints();
    private BoxLayout bx = null;//  @jve:decl-index=0:
    private int count = 1, i = 0;

    public MakeScrollablePanel() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Show();
        add(jButton11, BorderLayout.NORTH);
        add(scroll);
        setTitle("Priyank Panel");
        pack();
        setVisible(true);
        setLocationRelativeTo(null);
        setResizable(true);
    }

    private void Show() {
        jButton11 = new JButton("Add Designation");
        panel = new JPanel();
        bx = new BoxLayout(panel, BoxLayout.Y_AXIS);
        scroll = new JScrollPane(panel);
        dime5 = new Dimension(500, 150);
        panelConstraints = new GridBagConstraints();
        scroll.setPreferredSize(dime5);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        panel.setLayout(bx);
        panel.add(Box.createHorizontalBox());
        panel.setBorder(LineBorder.createBlackLineBorder());
        panel.setBackground(new Color(204, 230, 255));
        jButton11.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == jButton11) {
            label = new JLabel("Add Designation " + count + " :-");
            jTextFields = new JTextField(30);
            panel1 = new JPanel();
            panel1.setBackground(new Color(204, 230, 255));
            panel1.add(label);
            panel1.add(jTextFields);
            panel.add(panel1);
            panel.revalidate();
            panel.repaint();
            count++;
            i++;
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MakeScrollablePanel();
            }
        });
    }
}
import java.awt.*;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入javax.swing.*;
导入javax.swing.SwingUtilities;
导入javax.swing.border.LineBorder;
公共类MakeScrollablePanel扩展JFrame实现ActionListener{
私有JButton jButton11、jButton12;
私人JPanel小组,第1小组;
私有JTextField jTextFields;
私人标签;
私人JComboBox JComboBox;
私有维度dime,dime1,dime2,dime3,dime4,dime5;
私有JScrollPane滚动条;
私有GridBagConstraints panelConstraints=新GridBagConstraints();
private-BoxLayout bx=null;/@jve:decl-index=0:
私有整数计数=1,i=0;
公共MakeScrollablePanel(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Show();
添加(jButton11,BorderLayout.NORTH);
添加(滚动);
setTitle(“Priyank小组”);
包装();
setVisible(真);
setLocationRelativeTo(空);
可设置大小(真);
}
私人电视节目(){
jButton11=新JButton(“添加名称”);
panel=新的JPanel();
bx=新的BoxLayout(面板,BoxLayout.Y_轴);
滚动=新的JScrollPane(面板);
尺寸5=新尺寸(500150);
panelConstraints=新的GridBagConstraints();
滚动。设置首选大小(dime5);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u根据需要);
面板设置布局(bx);
panel.add(Box.createHorizontalBox());
panel.setOrder(LineBorder.createBlackLineBorder());
面板.立根背景(新颜色(204230255));
jButton11.addActionListener(这个);
}
@凌驾
已执行的公共无效操作(操作事件){
if(event.getSource()==jButton11){
标签=新的JLabel(“添加名称”+计数+:-”;
jTextFields=新的JTextField(30);
panel1=新的JPanel();
镶板1.立根背景(新颜色(204230255));
面板1.添加(标签);
panel1.添加(jTextFields);
小组。添加(小组1);
panel.revalidate();
panel.repaint();
计数++;
i++;
}
}
公共静态void main(字符串[]args){
SwingUtilities.invokeLater(新的Runnable(){
@凌驾
公开募捐{
新的MakeScrollablePanel();
}
});
}
}

此方法不起作用,我需要设置布局,不需要重新绘制和重新验证面板。
event.getSource()==jButton11
是个坏主意,使用
event.getSource().equals(jButton11)
.upda