【问题标题】:setting componenets by using GridBagLayout使用 GridBagLayout 设置组件
【发布时间】:2017-01-10 20:32:49
【问题描述】:

我正在使用 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();
    }
}

【问题讨论】:

  • 以标准尺寸提供 ASCII 艺术或 GUI 预期布局的简单绘图,并提供更多宽度和高度以显示应如何分配额外空间。
  • 没听懂,能否请您进一步解释一下?
  • @HasnainAhmadKhan 您能否提供一个示例,说明您的预期输出和当前输出以进行比较
  • 我期待标签“RegNam”左上角,即 x=0 & y=0。但它显示在框架的中心
  • @HasnainAhmadKhan 您误解了布局管理的工作原理,它不是基于像素的。我建议您先详细了解Laying Out Components Within a Container

标签: java swing layout-manager gridbaglayout


【解决方案1】:

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() 放在定义的末尾而不是开头会有所帮助。

【讨论】:

  • 是的,我认为你是对的,我又从头开始研究布局管理器。 ://
猜你喜欢
  • 2015-08-21
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
相关资源
最近更新 更多