理解GridBagConstraints gridx和gridy

时间:2014-10-20 18:14:21

标签: java swing gridbaglayout

我正在尝试使用gridx和gridy约束创建一个接口,如下所示(忽略Next按钮)。前两个JLabel-JTextField对工作正常,但我不确定我为剩余的10个字段设置了什么gridx和gridy值。

enter image description here

代码:

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

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

public class InitialPanel extends JPanel {

    private static final long serialVersionUID = -3159520372145273536L;

    /**
     * @param gridBagLayout
     */
    public InitialPanel() {

        super();

        initComponents();
    }

    /**
     * For each component to be added to this container:
     * 
     * 1. Create the component 
     * 2. Set instance variables in the GridBagConstraints instance
     * 3. Add component to panel.
     * 
     */
    private void initComponents() {

        this.setLayout(new GridBagLayout());

        GridBagConstraints gridBagConstraints = new GridBagConstraints();

        // All mandatory fields.

        // 1.

        gridBagConstraints.gridx = 0;

        gridBagConstraints.gridy = 0;

        JLabel hwCodeLabel = new JLabel("HW CODE:");

        this.add(hwCodeLabel, gridBagConstraints);


        JTextField hwCodeTextField = new JTextField("");

        gridBagConstraints.gridx = 1;

        gridBagConstraints.gridy = 0;

        hwCodeTextField.setColumns(10);

        this.add(hwCodeTextField, gridBagConstraints);


        // 2.

        JLabel publisherIdLabel = new JLabel("PUBLISHER ID:");

        gridBagConstraints.gridx = 0;

        gridBagConstraints.gridy = 1;

        this.add(publisherIdLabel, gridBagConstraints);



        JTextField publisherIdTextField = new JTextField("");

        gridBagConstraints.gridx = 1;

        gridBagConstraints.gridy = 1;

        publisherIdTextField.setColumns(10);

        this.add(publisherIdTextField,gridBagConstraints);



        /*// 3.
        JLabel journalTitleLabel = new JLabel("JOURNAL TITLE:");

        gridBagConstraints.gridx = 0;

        gridBagConstraints.gridy = 2;

        this.add(journalTitleLabel,gridBagConstraints);




        JTextField journalTitleTextField = new JTextField();

        gridBagConstraints.gridx = 2;

        gridBagConstraints.gridy = 2;

        journalTitleTextField.setColumns(10);

        this.add(journalTitleTextField,gridBagConstraints);


        // 4.


        JLabel printISSNLabel = new JLabel("PRINT ISSN:");

        gridBagConstraints.gridx = 3;

        gridBagConstraints.gridy = 1;

        this.add(printISSNLabel,gridBagConstraints);



        JTextField printISSNTextField = new JTextField();

        gridBagConstraints.gridx = 3;

        gridBagConstraints.gridy = 2;

        journalTitleTextField.setColumns(10);

        this.add(printISSNTextField,gridBagConstraints);


        // 5.

        JLabel eISSNLabel = new JLabel("ELECTRONIC ISSN:");

        gridBagConstraints.gridx = 4;

        gridBagConstraints.gridy = 1;

        this.add(eISSNLabel,gridBagConstraints);


        JTextField eISSNTextField = new JTextField();

        gridBagConstraints.gridx = 4;

        gridBagConstraints.gridy = 2;

        journalTitleTextField.setColumns(10);

        this.add(eISSNTextField,gridBagConstraints);


        // 6.

        JLabel publisherNameLabel = new JLabel("PUBLISHER NAME:");

        gridBagConstraints.gridx = 5;

        gridBagConstraints.gridy = 1;

        this.add(publisherNameLabel,gridBagConstraints);



        JTextField publisherNameTextField = new JTextField();

        gridBagConstraints.gridx = 5;

        gridBagConstraints.gridy = 2;

        journalTitleTextField.setColumns(10);

        this.add(publisherNameTextField,gridBagConstraints);



        // 7.

        JLabel publisherLOCLabel = new JLabel("PUBLISHER LOCATION:");

        gridBagConstraints.gridx = 6;

        gridBagConstraints.gridy = 1;

        this.add(publisherLOCLabel,gridBagConstraints);



        JTextField publisherLOCTextField = new JTextField();

        gridBagConstraints.gridx = 6;

        gridBagConstraints.gridy = 2;

        journalTitleTextField.setColumns(10);

        this.add(publisherLOCTextField,gridBagConstraints);



        // 8.

        JLabel coverDateLabel = new JLabel("COVER DATE:");

        gridBagConstraints.gridx = 7;

        gridBagConstraints.gridy = 1;

        this.add(coverDateLabel,gridBagConstraints);


        JTextField coverDateTextField = new JTextField();

        gridBagConstraints.gridx = 7;

        gridBagConstraints.gridy = 2;

        journalTitleTextField.setColumns(10);

        this.add(coverDateTextField,gridBagConstraints);


        // 9.

        JLabel volumeLabel = new JLabel("VOLUME:");

        gridBagConstraints.gridx = 8;

        gridBagConstraints.gridy = 1;

        this.add(volumeLabel,gridBagConstraints);



        JTextField volumeTextField = new JTextField();

        gridBagConstraints.gridx = 8;

        gridBagConstraints.gridy = 2;

        journalTitleTextField.setColumns(10);

        this.add(volumeTextField,gridBagConstraints);


        // 10.

        JLabel issueLabel = new JLabel("ISSUE:");

        gridBagConstraints.gridx = 9;

        gridBagConstraints.gridy = 1;

        this.add(issueLabel,gridBagConstraints);



        JTextField issueTextField = new JTextField();

        gridBagConstraints.gridx = 9;

        gridBagConstraints.gridy = 2;

        journalTitleTextField.setColumns(10);

        this.add(issueTextField,gridBagConstraints);


        // 11.

        JLabel copyrightStatementLabel = new JLabel("COPYRIGHT STATEMENT:");

        gridBagConstraints.gridx = 10;

        gridBagConstraints.gridy = 1;

        this.add(copyrightStatementLabel,gridBagConstraints);



        JTextField copyrightStatementTextField = new JTextField();

        gridBagConstraints.gridx = 10;

        gridBagConstraints.gridy = 2;

        journalTitleTextField.setColumns(10);

        this.add(copyrightStatementTextField,gridBagConstraints);


        // 12.

        JLabel copyRightYearLabel = new JLabel("COPYRIGHT YEAR:");

        gridBagConstraints.gridx = 11;

        gridBagConstraints.gridy = 1;

        this.add(copyRightYearLabel,gridBagConstraints);


        JTextField copyRightYearTextField = new JTextField();

        gridBagConstraints.gridx = 11;

        gridBagConstraints.gridy = 2;

        journalTitleTextField.setColumns(10);

        this.add(copyRightYearTextField,gridBagConstraints);
*/
    }

}

2 个答案:

答案 0 :(得分:2)

我在这里修改了你的程序以获得所需的结果,我还添加了两种方法来使你的代码更具可读性和效率:

public class InitialPanel extends JPanel {

    private static final long serialVersionUID = -3159520372145273536L;

    public InitialPanel() {
        super();
        initComponents();
    }

    private void initComponents() {

        this.setLayout(new GridBagLayout());

        GridBagConstraints gridBagConstraints = new GridBagConstraints();

        // All mandatory fields.
        // 1.
        JLabel hwCodeLabel = addMyLabelCell(0, "HW CODE:");
        JTextField hwCodeTextField = addMyTextFieldCell(0, "");

        // 2.
        JLabel publisherIdLabel = addMyLabelCell(1, "PUBLISHER ID:");
        JTextField publisherIdTextField = addMyTextFieldCell(1, "");

        // 3.
        JLabel journalTitleLabel = addMyLabelCell(2, "JOURNAL TITLE:");
        JTextField journalTitleTextField = addMyTextFieldCell(2, "");

        // 4.
        JLabel printISSNLabel = addMyLabelCell(3, "PRINT ISSN:");
        JTextField printISSNTextField = addMyTextFieldCell(3, "");

        // 5.
        JLabel eISSNLabel = addMyLabelCell(4, "ELECTRONIC ISSN:");
        JTextField eISSNTextField = addMyTextFieldCell(4, "");

        // 6.
        JLabel publisherNameLabel = addMyLabelCell(5, "PUBLISHER NAME:");
        JTextField publisherNameTextField = addMyTextFieldCell(5, "");

        // 7.
        JLabel publisherLOCLabel = addMyLabelCell(6, "PUBLISHER LOCATION:");
        JTextField publisherLOCTextField = addMyTextFieldCell(6, "");

        // 8.
        JLabel coverDateLabel = addMyLabelCell(7, "COVER DATE:");
        JTextField coverDateTextField = addMyTextFieldCell(7, "");

        // 9.
        JLabel volumeLabel = addMyLabelCell(8, "VOLUME:");
        JTextField volumeTextField = addMyTextFieldCell(8, "");

        // 10.
        JLabel issueLabel = addMyLabelCell(9, "ISSUE:");
        JTextField issueTextField = addMyTextFieldCell(9, "");

        // 11.
        JLabel copyrightStatementLabel = addMyLabelCell(10, "COPYRIGHT STATEMENT:");
        JTextField copyrightStatementTextField = addMyTextFieldCell(10, "");

        // 12.
        JLabel copyRightYearLabel = addMyLabelCell(11, "COPYRIGHT YEAR:");
        JTextField copyRightYearTextField = addMyTextFieldCell(11, "");
    }

    public JLabel addMyLabelCell(int pos, String labelstr) {
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = pos;
        JLabel label = new JLabel(labelstr);
        this.add(label, gridBagConstraints);
        return label;
    }

    public JTextField addMyTextFieldCell(int pos, String text) {
        GridBagConstraints gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.anchor = GridBagConstraints.EAST;
        gridBagConstraints.ipadx = 10;
        JTextField textField = new JTextField(text);
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = pos;
        textField.setColumns(10);
        this.add(textField, gridBagConstraints);
        return textField;
    }

    public static void main(String... args) {
        SwingUtilities.invokeLater(() -> {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(new InitialPanel());
            f.setSize(300, 300);
            f.setVisible(true);
        });
    }
}

这是输出:

enter image description here

答案 1 :(得分:1)

对于所有行,gridy应为行号,gridx对于标签为0,对于文本字段为1。

更正您的代码并验证这是否可以解决您的问题