使用GridBagLayout设置组件

时间:2017-01-10 20:32:50

标签: java swing layout-manager gridbaglayout

我正在使用GridBagLayout来定位面板上的组件,但它不像它一样工作。通过改变x和y值不影响组件的位置有人请帮助解释我在这里犯的错误提前感谢:)

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

import javax.swing.*;

public class LMS extends JFrame {
    JPanel mainPanel;
    JLabel RegNum, Name, FatherNam, MotherNam, DateBirth, BloodGrp, Email, Gender, RegDate, Desig, photo;
    JTextField RegNuumText, FatherNamText, MotherNamText, EmailText, DesigText;
    JList BloodGrpList;
    JSpinner DateSpi;

    // Constructor
    public LMS() {
        this.setTitle("Library Managment System");
        this.setVisible(true);
        this.setSize(700, 600);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);

        // this.setResizable(false);
        mainPanel = new JPanel();

        // GridBagLayout bag = new GridBagLayout();
        mainPanel.setLayout(new GridBagLayout());

        // Creating Labels
        RegNum = new JLabel("Registration Number");
        Name = new JLabel("Full Name");
        FatherNam = new JLabel("Father's Name");
        MotherNam = new JLabel("Mother's Name");
        DateBirth = new JLabel("Date Of Birth");
        BloodGrp = new JLabel("Blood Group");
        Email = new JLabel("Email");
        Gender = new JLabel("Gender");
        RegDate = new JLabel("Registration Date");
        Desig = new JLabel("Designation");
        photo = new JLabel("Photo");

        // creating Text Fields
        RegNuumText = new JTextField(30);
        FatherNamText = new JTextField();
        MotherNamText = new JTextField();
        EmailText = new JTextField();
        DesigText = new JTextField();

        // mainPanel.add(RegNum);
        addComp(mainPanel, RegNum, 0, 0, 2, 1, GridBagConstraints.EAST, GridBagConstraints.NONE);
        addComp(mainPanel, RegNuumText, 0, 1, 2, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
        this.add(mainPanel);
    }

    private void addComp(JPanel thePanel, JComponent comp, int xPos, 
            int yPos, int compWidth, int compHeight, int place, int stretch) {

        GridBagConstraints gridConstraints = new GridBagConstraints();

        gridConstraints.gridx = xPos;
        gridConstraints.gridy = yPos;
        gridConstraints.gridwidth = compWidth;
        gridConstraints.gridheight = compHeight;
        gridConstraints.weightx = 1;
        gridConstraints.weighty = 1;
        gridConstraints.insets = new Insets(5, 5, 5, 5);
        gridConstraints.anchor = place;
        gridConstraints.fill = stretch;

        thePanel.add(comp, gridConstraints);
    }

    public static void main(String[] args) {
        new LMS();
    }
}

1 个答案:

答案 0 :(得分:0)

GridBagLayout不是最容易的,你必须玩一点。也许这些线条可以帮助您实现您想要的效果。它们会将标签放在左上角,而Textfield就在它后面。

请注意,有些面板被定义为占位符 - 它们将填满空白区域。

    addComp(mainPanel, RegNum, 0, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
    addComp(mainPanel, RegNuumText, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE);
    addComp(mainPanel, new JPanel(), 2, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL);
    addComp(mainPanel, new JPanel(), 0, 2, 1, 1, GridBagConstraints.WEST, GridBagConstraints.VERTICAL);

您还需要稍微更改辅助方法:

    gridConstraints.weightx = stretch == GridBagConstraints.NONE || stretch == GridBagConstraints.VERTICAL ? 0 : 1;
    gridConstraints.weighty = stretch == GridBagConstraints.NONE || stretch == GridBagConstraints.HORIZONTAL ? 0 : 1;

我认为这应该可以解决问题。也许您应该定义两个或三个辅助方法,因此您不需要每次都设置所有参数。根据我的经验,您很少需要定义锚点,而且有时您可能需要拉伸组件。大多数时候,对我来说,只是将东西放入正确的袋子里(x,y,有时候:宽度,高度)。

编辑:也许将setVisible()放在定义的末尾,而不是开头。