简体   繁体   English

将组件添加到我的GridBagLayout中会将其他组件移到右侧

[英]Adding component to my GridBagLayout moves other components far to the right

I'm seeing some really weird results when I try to lay out my JPanel . 当我尝试布局JPanel时,我看到了一些非常奇怪的结果。 Everything works fine before I add the last JTextField subline : 一切正常之前,我添加了最后JTextField subline

添加最后一个JTextField之前的正确布局

When I add that, the JTextField above it moves to the right, so that it begins where subline ends: 当我再补充一点,在JTextField它上面移动到右侧,以便它开始的地方subline结束:

添加最后一个JTextField后布局不正确

Here's the code that creates this layout: 这是创建此布局的代码:

public class Opspanel extends JPanel{
    private static final long serialVersionUID = -6393281054430179953L;
    public Opspanel() {
        GridBagLayout layout = new GridBagLayout();
        setLayout(layout);
        GridBagConstraints constraints = new GridBagConstraints();

        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.anchor = GridBagConstraints.LINE_START;
        add(new JLabel("Vendor ID"), constraints);

        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.anchor = GridBagConstraints.LINE_START;
        JTextField vendorid = new JTextField();
        vendorid.setPreferredSize(new Dimension(100,20));
        add(vendorid, constraints);

        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.anchor = GridBagConstraints.LINE_START;
        add(new JLabel("Email Date"), constraints);

        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.anchor = GridBagConstraints.LINE_START;
        JTextField emaildate = new JTextField();
        emaildate.setPreferredSize(new Dimension(100,20));
        add(emaildate, constraints);

        constraints.gridx = 2;
        constraints.gridy = 1;
        constraints.anchor = GridBagConstraints.LINE_START;
        add(new JLabel("Email Time"), constraints);

        constraints.gridx = 3;
        constraints.gridy = 1;
        constraints.anchor = GridBagConstraints.LINE_START;
        JTextField emailtime = new JTextField();
        emailtime.setPreferredSize(new Dimension(100,20));
        add(emailtime, constraints);

        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.anchor = GridBagConstraints.LINE_START;
        add(new JLabel("Subject Line"), constraints);

        constraints.gridx = 1;
        constraints.gridy = 2;
        constraints.anchor = GridBagConstraints.LINE_START;
        JTextField subline = new JTextField();
        subline.setPreferredSize(new Dimension(500, 20));
        add(subline, constraints);  

    }
}

How can I fix this? 我怎样才能解决这个问题?

According to the docs for GridBagLayout , 根据GridBagLayout的文档

Each GridBagLayout object maintains a dynamic, rectangular grid of cells, with each component occupying one or more cells, called its display area. 每个GridBagLayout对象维护一个动态的矩形单元格网格,每个组件占据一个或多个单元格,称为其显示区域。

In your code, your grid has 3 rows and 4 columns. 在您的代码中,网格有3行4列。 You're telling each of your components to occupy exactly one cell, in the position given by gridx and gridy . 您要告诉每个组件在gridxgridy给定的位置恰好占据一个单元。

The fact that you don't like some of them "stopping" or "starting" where they do indicates that you really wanted some of them to occupy more than one cell. 您可能不喜欢其中的某些“停止”或“开始”,这一事实表明您确实希望其中的一些占据一个以上的单元。

You're in luck! 你真幸运! GridBagLayout can do that. GridBagLayout可以做到这一点。 You're looking for GridBagLayout.gridwidth : 您正在寻找GridBagLayout.gridwidth

Specifies the number of cells in a row for the component's display area. 指定组件显示区域中一行的单元格数。

Use REMAINDER to specify that the component's display area will be from gridx to the last cell in the row. 使用REMAINDER可以指定组件的显示区域是从gridx到行的最后一个单元格。 Use RELATIVE to specify that the component's display area will be from gridx to the next to the last one in its row. 使用RELATIVE可以指定组件的显示区域是从gridx到其行中最后一个区域的下一个区域。

gridwidth should be non-negative and the default value is 1. gridwidth应该为非负值,默认值为1。


In your example, you put the "Subject Line" text field in the second column ( gridx = 1 ). 在您的示例中,将“主题行”文本字段放在第二列( gridx = 1 )。 Since you left gridwidth at its default value of 1 , that text field cannot overlap the third or fourth columns. 由于您将gridwidth保留为其默认值1 ,因此该文本字段不能与第三或第四列重叠。 So, if it grows, the whole column must grow with it. 因此,如果增长,则整个列都必须随之增长。

You can let it fill the whole horizontal space by letting it cover multiple grid cells. 您可以通过覆盖多个网格单元来填充整个水平空间。 (If you're familiar with Microsoft Excel or tables in Microsoft Word or similar programs, this is like the "Merge Cells" option.) (如果您熟悉Microsoft Excel或Microsoft Word或类似程序中的表,则类似于“合并单元格”选项。)

To make it cover 3 columns, starting with gridx = 1 : 为了使其覆盖3列,从gridx = 1

constraints.gridx = 1;
constraints.gridwidth = 3;

Or alternatively, to make it cover all cells from column gridx = 1 to the right: 或者,使其覆盖从列gridx = 1到右侧的所有单元格:

constraints.gridx = 1;
constraints.gridwidth = GridBagLayout.REMAINDER;

Just don't forget to reset gridwidth to 1 before you use it to add another component that's supposed to cover only one column! 只是不要忘记在使用它来添加另一个本应仅覆盖一列的组件之前将gridwidth重置为1!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM