2

我需要创建一个 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 行:

评论行[87]

未注释的第 87 行:

未注释行[87]

4

1 回答 1

2

左列宽度中的 GridBagConstraints.RELATIVE 表示仅在其右侧留出一列空间。您可能希望将该按钮的宽度更改为1.

边注:

不应该是这样的:

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

于 2013-10-11T20:01:53.973 回答