简体   繁体   English

GridBagLayout组件显示不正确的宽度

[英]GridBagLayout components display incorrect width

I seem to be missing something pretty basic about the GridBagLayout. 我似乎缺少有关GridBagLayout的一些基本知识。 I'm trying to specify that a label should be 2 units wide, and that a text field should be 10 units wide. 我试图指定标签的宽度应为2个单位,文本字段的宽度应为10个单位。

The following code imitates part of what I'm doing. 以下代码模仿了我正在做的一部分。 It shows the text field and label taking up about the same amount of width. 它显示文本字段和标签占用的宽度大致相同。 What am I missing or doing incorrectly? 我缺少什么或做错了什么?

package numbers;

import javax.swing.*;
import java.awt.*;

public class FooBar extends JFrame {
    public static void main(String[] args) {
        new FooBar();
    }
    FooBar() {
        super("Foobar");
        setSize(600,100);

        JPanel panel = new JPanel();
        setLayout(new GridBagLayout());

        JLabel nLabel = new JLabel("Foo");
        JTextField nTextField = new JTextField("Bar");

        GridBagConstraints c = new GridBagConstraints();

        add(nLabel,         cs(0,  0,  2, 1, c.CENTER, c.NONE));
        add(nTextField,     cs(2,  0, 10, 1, c.CENTER, c.HORIZONTAL));

        add(panel);
        setVisible(true);
    }

    private GridBagConstraints cs(int gridx, int gridy,
            int gridwidth, int gridheight,
            int anchor, int fill) {
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = gridx;
        c.gridy = gridy;
        c.gridwidth = gridwidth;
        c.gridheight = gridheight;
        c.anchor = anchor;
        c.fill = fill;

        c.weightx = 1;
        c.weighty = 1;
        return c;
    }
}

I thought I'd specified that nLabel's gridwidth is 2, and that nTextField's gridwidth is 10. I would think the columns would be of about the same width they have the same weightx. 我以为我已指定nLabel的gridwidth为2,nTextField的gridwidth为10。我认为这些列的宽度大约相同,它们具有相同的weightx。 Why, then, do the components have the same width? 那么,为什么这些零件具有相同的宽度? And why are the columns apparently of different sizes? 为何色谱柱的尺寸明显不同?

Update: 更新:

adding weightx = 2 and weightx = 10 seems to fix this snippet… but it doesn't work in a larger context. 添加weightx = 2和weightx = 10似乎可以解决此代码段……但在更大的范围内不起作用。 The code I'm using looks something like this: 我正在使用的代码如下所示:

    add(intro1,         cs(0,  0, 12, 1, c.WEST,    c.NONE));
    add(intro2,         cs(0,  1, 12, 1, c.WEST,    c.NONE));
    add(nLabel,         cs(0,  3,  2, 1, c.CENTER,  c.NONE));
    add(nTextField,     cs(2,  3, 10, 1, c.CENTER,  c.HORIZONTAL));
    add(nthFoundLabel,  cs(0,  4,  3, 1, c.CENTER,  c.NONE));
    add(nFoundText,     cs(2,  4, 10, 1, c.CENTER,  c.HORIZONTAL));
    add(blankLine,      cs(0,  5, 12, 1, c.CENTER,  c.HORIZONTAL));
    add(startButton,    cs(0,  6,  6, 1, c.CENTER,  c.NONE));
    add(stopButton,     cs(6,  6,  6, 1, c.CENTER,  c.NONE));
    add(blankLine,      cs(0,  7, 12, 1, c.CENTER,  c.BOTH));
    add(progressLabel,  cs(0,  8, 12, 1, c.CENTER,  c.NONE));
    add(lastFoundLabel, cs(0,  9,  3, 1, c.CENTER,  c.NONE));
    add(lastFoundText,  cs(3,  9,  9, 1, c.CENTER,  c.HORIZONTAL));
    add(lastNText,      cs(3, 10,  9, 1, c.CENTER,  c.HORIZONTAL));
    add(elapsedLabel,   cs(0, 11,  3, 1, c.CENTER,  c.HORIZONTAL));
    add(remainingLabel, cs(0, 12,  3, 1, c.CENTER,  c.HORIZONTAL));
    add(progressBar,    cs(0, 13, 12, 1, c.CENTER,  c.HORIZONTAL));

I thought this might be fixed by changing the helper method cs by adding weightx = gridwidth; 我认为可以通过添加weightx = gridwidth;来更改辅助方法cs来解决此问题weightx = gridwidth; but this still doesn't resolve the problem. 但这仍然不能解决问题。 What on earth is going on? 到底是怎么回事?

The distribution of container width to each child component is determined by weightx. 容器宽度到每个子组件的分布由weightx确定。 The values you are assigning to gridwidth should be assigned to weightx instead. 您分配给gridwidth的值应该分配给weightx。 The gridwidth should be 1 (or REMAINDER) for all of your components. 所有组件的网格宽度应为1(或其余)。

gridwidth and gridheight do not change the size at which a component is laid out. gridwidth和gridheight不会更改组件布局的大小。 In particular, a component with gridwidth=2 is not twice as wide as a component with gridwidth=1. 特别地,具有gridwidth = 2的组分是两倍宽与gridwidth = 1的组分。

Despite the word "Grid" in the name, a GridBagLayout's cells are not all the same size; 尽管名称中有“ Grid”一词,但GridBagLayout的单元格并非全部相同。 they are merely aligned together. 它们只是对齐在一起。 If you drew lines to show each cell, a GridBagLayout might look like this: 如果绘制线条以显示每个单元格,则GridBagLayout可能如下所示:

_________________________________
            |         |               |       |
 (gridy=0)  | gridx=1 |    gridx=2    |gridx=3|
            |_________|_______________|_______|
            |         |               |       |
            |         |               |       |
 (gridy=1)  | gridx=1 |    gridx=2    |gridx=3|
            |         |               |       |
            |_________|_______________|_______|

gridwidth/gridheight determine how many of the GridBagLayout's cells are occupied by the component. gridwidth / gridheight确定该组件占用了GridBagLayout的多少个单元格。 If you associate gridwidth=2 with a component, but there are no other components in either of the grid columns, the effect will be identical to gridwidth=1, because no other components are driving the widths of the columns. 如果将gridwidth = 2与某个组件相关联,但是在任一网格列中都没有其他组件,则效果将与gridwidth = 1相同,因为没有其他组件在驱动列宽。 Even though a gridwidth=2 component is two cells wide, those two cells are still only going to be laid out with enough total width to accommodate the component's preferred size. 即使gridwidth = 2组件的宽度为两个单元,但仍将仅以足够的总宽度来布局这两个单元,以适应该组件的首选大小。

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

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