Swing:GridBagLayouts 锚点嵌套在其他 GridBagLayout 中不起作用
Swing: GridBagLayouts anchor does not work nested in an other GridBagLayout
我正在将第一个 GridBagLayout
设置为主 JPanel
。对于此面板,使用此布局 (outerConstraints) 添加了其他十个面板。子面板包含一定数量的文本,应使用新布局和 innerConstraints
将其放置在西边。但他们居中。
稍加测试后,我发现文本/图像被放置在西边,但前提是 outerConstraints
不水平填充白色面板。问题是,我无法删除此命令,因为我需要每个面板都具有相同的宽度。
是否有可能允许 outerConstraints
填充并使其嵌套 GridBagLayout
将文本定位在左侧?
setLayout(new GridBagLayout());
GridBagConstraints outerConstraints = new GridBagConstraints();
outerConstraints.insets = new Insets(5, 5, 5, 5);
outerConstraints.fill = GridBagConstraints.HORIZONTAL; // seems to be reason, but needed.
for (int i = 0; i < 10; i++) {
outerConstraints.gridx = i % 2;
outerConstraints.gridy = i / 2;
JPanel agentVisitCard = new JPanel();
add(agentVisitCard, outerConstraints);
GridBagConstraints innerConstraints = new GridBagConstraints();
innerConstraints.anchor = GridBagConstraints.WEST; //fails
innerConstraints.insets = new Insets(5, 5, 5, 5);
GridBagLayout layout = new GridBagLayout();
agentVisitCard.setLayout(layout);
(...)
JLabel label = new JLabel("Agent " + (i + 1) + ":");
innerConstraints.gridx = 0;
innerConstraints.gridy = 0;
agentVisitCard.add(label, innerConstraints);
代理 3 没有左对齐:
锚不工作的原因是组件没有重量。否则,给组件的区域只是所需的最小尺寸,即组件本身的尺寸,不会更大。
这里引自 swing tutorials(在 weightx, weighty 部分)解释它:
Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.
默认情况下,weightx 和 weighty 为 0,这就是它不适合您的原因。由于您只想水平锚定它,因此将 weightx 设置为非零值可以使它起作用。如果你想要一个垂直锚点,你也需要设置权重。
关于如何使用 weightx 和 weighty 值的解释如下:
Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary. Larger numbers indicate that the component's row or column should get more space.
虽然他们建议使用 0.0 到 1.0 之间的值,但这不是必需的。任何 double
值都可以。如果你有多个组件,重要的是组件的权重之间的比率。
我正在将第一个 GridBagLayout
设置为主 JPanel
。对于此面板,使用此布局 (outerConstraints) 添加了其他十个面板。子面板包含一定数量的文本,应使用新布局和 innerConstraints
将其放置在西边。但他们居中。
稍加测试后,我发现文本/图像被放置在西边,但前提是 outerConstraints
不水平填充白色面板。问题是,我无法删除此命令,因为我需要每个面板都具有相同的宽度。
是否有可能允许 outerConstraints
填充并使其嵌套 GridBagLayout
将文本定位在左侧?
setLayout(new GridBagLayout());
GridBagConstraints outerConstraints = new GridBagConstraints();
outerConstraints.insets = new Insets(5, 5, 5, 5);
outerConstraints.fill = GridBagConstraints.HORIZONTAL; // seems to be reason, but needed.
for (int i = 0; i < 10; i++) {
outerConstraints.gridx = i % 2;
outerConstraints.gridy = i / 2;
JPanel agentVisitCard = new JPanel();
add(agentVisitCard, outerConstraints);
GridBagConstraints innerConstraints = new GridBagConstraints();
innerConstraints.anchor = GridBagConstraints.WEST; //fails
innerConstraints.insets = new Insets(5, 5, 5, 5);
GridBagLayout layout = new GridBagLayout();
agentVisitCard.setLayout(layout);
(...)
JLabel label = new JLabel("Agent " + (i + 1) + ":");
innerConstraints.gridx = 0;
innerConstraints.gridy = 0;
agentVisitCard.add(label, innerConstraints);
代理 3 没有左对齐:
锚不工作的原因是组件没有重量。否则,给组件的区域只是所需的最小尺寸,即组件本身的尺寸,不会更大。
这里引自 swing tutorials(在 weightx, weighty 部分)解释它:
Unless you specify at least one non-zero value for weightx or weighty, all the components clump together in the center of their container. This is because when the weight is 0.0 (the default), the GridBagLayout puts any extra space between its grid of cells and the edges of the container.
默认情况下,weightx 和 weighty 为 0,这就是它不适合您的原因。由于您只想水平锚定它,因此将 weightx 设置为非零值可以使它起作用。如果你想要一个垂直锚点,你也需要设置权重。
关于如何使用 weightx 和 weighty 值的解释如下:
Generally weights are specified with 0.0 and 1.0 as the extremes: the numbers in between are used as necessary. Larger numbers indicate that the component's row or column should get more space.
虽然他们建议使用 0.0 到 1.0 之间的值,但这不是必需的。任何 double
值都可以。如果你有多个组件,重要的是组件的权重之间的比率。