为什么GridBagLayout不创建第三列?

时间:2013-10-11 19:54:44

标签: java swing gridbaglayout

我需要创建一个包含3列和5行的表单

+---+-----------+
|   |           |
|   +-----+-----+
|   |     |     |
|   +-----+-----+
|   |     |     |
|   +-----+-----+
|   |     |     |
|   +-----+-----+
|   |     |     |
+---+-----+-----+

但是GridBagLayout将所有元素放在2列的形式中:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Lab7_3 {

    public static void main(String[] args) {
        buildFrame("Groups");

    }

    public static void buildFrame(String name){
        JFrame fr = new JFrame(name);

        //Создание переключателя окна
            JButton JBMore=new JButton("<html>><br/>><br/>></html>");
        //Создание переключателя окна

        //Создание последней - информационной панели
            JPanel jp2 = new JPanel(new GridBagLayout());
            GridBagConstraints c = new GridBagConstraints();

            JLabel jl = new JLabel("Select the person");

                ArrayList<JLabel> arrLabel = new ArrayList<JLabel>();
                ArrayList<JTextField> arrText = new ArrayList<JTextField>();
                for (int i=0;i<4;i++){
                    arrLabel.add(new JLabel(""));
                    arrText.add(new JTextField(""));
                    //jp2_2.add(arrLabel.get(i));
                    //jp2_2.add(arrText.get(i));
                }
                arrLabel.get(0).setText("Age:");
                arrLabel.get(1).setText("Address:");
                arrLabel.get(2).setText("Telephone:");
                arrLabel.get(3).setText("Favorite color:");

            c.insets = new Insets(0,0,0,0);
            c.gridheight = GridBagConstraints.REMAINDER;
            c.gridwidth = GridBagConstraints.RELATIVE;
            c.anchor = GridBagConstraints.WEST;
            c.fill = GridBagConstraints.VERTICAL;
            c.weighty = 1.0;
            c.weightx = 0.1;
            c.gridx = 0;
            c.gridy = 0;
            c.ipadx = 0;
            jp2.add(JBMore,c);  

            c = new GridBagConstraints();
            c.insets = new Insets(10, 10, 20, 10);
            c.anchor = GridBagConstraints.CENTER;
            c.gridheight = 1;
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.weightx = 1;
            c.weighty = 0.01;
            c.gridx = 1;
            c.gridy = 0;
            jp2.add(jl,c);

            c = new GridBagConstraints();
            c.insets = new Insets(2,10,10,10);
            c.gridwidth = 1;
            c.gridheight = 1;
            for (int i=0;i<4;i++){
                c.anchor = GridBagConstraints.WEST;
                c.fill = GridBagConstraints.NONE;
                c.gridx = 1;
                c.weightx = 0.05;
                c.gridy = 1+i;
                c.weighty = 0.05;
                c.ipadx = 0;
                jp2.add(arrLabel.get(i),c);

                c.anchor = GridBagConstraints.EAST;
                c.fill = GridBagConstraints.VERTICAL;
                c.gridx = 2;
                c.weightx = 0.05;
                c.ipadx = 10;
                //jp2.add(arrText.get(i),c);
            }


        //Создание последней - информационной панели

        fr.add(jp2);

        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr.setVisible(true);
        fr.pack();
        fr.setMinimumSize(fr.getSize());        
    }

}

尝试取消注释这一行[87]:

//jp2.add (arrText.get (i), c);

您将看到所有元素都向左移动


评论第87行:

Commented line[87]

未注释第87行:

Uncommented line[87]

1 个答案:

答案 0 :(得分:2)

左栏宽度中的GridBagConstraints.RELATIVE意味着只留下一列的右侧空间。您可能希望将该按钮的宽度更改为1

旁注:

不应该是这样的:

<html>&gt;<br/>&gt;<br/>&gt;</html>