java - 动态添加textField时的setlayout

标签 java swing jtable

我正在 panel1 中添加动态 JTextFieldJLabel 但我无法设置 JTextField 的布局> 和 JLabel。我需要将 JTextfieldJLabel 添加到 panel1 并将 panel1 添加到 panel .我需要以自上而下的方式添加 JTextFieldJLabel 并设置布局。 panel1panel 是 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();

            }
        });

    }

}

最佳答案

还是不行

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

enter image description here

.

enter image description here

.

enter image description here

来自(一点点)修改过的 OP 代码

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();
            }
        });
    }
}

关于java - 动态添加textField时的setlayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22681898/

相关文章:

java - 缺少 cxf jar : Please select the cxf home directory

Java 线程 - 获取 ServerSocket 输入来更新 Swing

java - JDK 未本地化 JComponent

java - 在 GRAL 中绘制多个图形

java - 无法使用 selenium webdriver 单击/鼠标悬停在元素上

java - 获取与 int 值关联的枚举

java - 如何将小型 ORC 文件合并或合并为大型 ORC 文件?

java - JTable Cell 内的 JDatechooser

Java JTable AttributiveCellTableModel 添加行

java - JTable.columnMoved 方法的一个错误