在 JFrame 上正确打包元素数组

Pack correctly arrays of elements on JFrame

我有简单的表单和两个数组,JLabels 和 JTextFeilds:

ArrayList<JLabel> jlabels = new ArrayList<JLabel>();
ArrayList<JTextField> textFields = new ArrayList<JTextField>();

我想将它添加到框架中并正确打包这些元素(请看图片)

请看我的一段代码:

public class ProductForm extends JFrame {

private JPanel contentPane; 

public ProductForm() {

    initComponents();
    actions();      
}

public void actions() {
    setTitle("Product selection");

    setSize(600, 350);
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setVisible(true);
}

private void initComponents() {
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);

    ArrayList<JLabel> jlabels = new ArrayList<JLabel>();
    ArrayList<JTextField> textFields = new ArrayList<JTextField>();

    //for (int a = 0; a < product.list.size(); a++) {
    for (int a = 0; a < 7; a++) {  // let say 7 for example
    jlabels.add(new javax.swing.JLabel()); 
    textFields.add(new javax.swing.JTextField());       
        for (JLabel j:jlabels) {
            contentPane.add(j);
        }
        for (JTextField f:textFields) {
            contentPane.add(f);
        }
    }             

    pack();
}

结果,我得到了这个内容:请看下图

请帮助我如何正确打包这些元素。 我应该以某种方式初始化它吗?

感谢您的帮助。

您应该使用适当的布局。我建议使用 GridLayout.

在您的 initComponents() 方法中添加这一行:

contentPane.setLayout(new GridLayout(7,2)); // 7 rows, 2 columns

并将循环更改为以下内容:

for (int a = 0; a < 7; a++) {
    jlabels.add(new javax.swing.JLabel()); 
    textFields.add(new javax.swing.JTextField());
}             

for (int i=0; i<7; i++) {
    contentPane.add(jlabels.get(i));
    contentPane.add(textFields.get(i));
}

试试这个

private void initComponents() {
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new GridLayout(7, 2)); // 7 rows and 2 cols
            setContentPane(contentPane);

            ArrayList<JLabel> jlabels = new ArrayList<JLabel>();
            ArrayList<JTextField> textFields = new ArrayList<JTextField>();

            // for (int a = 0; a < product.list.size(); a++) {
            for (int a = 0; a < 7.; a++) { // let say 7 for example
                JLabel jLabel = new javax.swing.JLabel("New Label");
                jlabels.add(jLabel);
                JTextField jTextField = new javax.swing.JTextField();
                textFields.add(jTextField);
                contentPane.add(jLabel);
                contentPane.add(jTextField);
            }

            pack();
        }

更新

private void initComponents() {
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new GridBagLayout()); // 7 rows and 2 cols
        setContentPane(contentPane);

        GridBagConstraints constraints = new GridBagConstraints();
        constraints.anchor=GridBagConstraints.NORTHWEST;
        constraints.weightx=0.5;
        constraints.weighty=0.5;


        ArrayList<JLabel> jlabels = new ArrayList<JLabel>();
        ArrayList<JTextField> textFields = new ArrayList<JTextField>();

        // for (int a = 0; a < product.list.size(); a++) {
        for (int a = 0; a < 7.; a++) { // let say 7 for example
            JLabel jLabel = new javax.swing.JLabel("New Label");
            constraints.gridx=0;
            constraints.gridy=a;
            jlabels.add(jLabel);
            JTextField jTextField = new javax.swing.JTextField(10);
            textFields.add(jTextField);
            contentPane.add(jLabel, constraints);
            constraints.gridx=1;
            contentPane.add(jTextField, constraints);
        }

        pack();
    }