gridwidth 和 gridheight 不工作

gridwidth and gridheight aren't working

我正在 java 中使用 GridBagLayout,我编写了以下代码,但它没有像我预期的那样工作
我预计第一个按钮将放置在框架左上角的 (0,0) 点。
其次,我预计第二个按钮的高度和宽度将是第一个按钮高度和宽度的两倍,但事实并非如此。
这是输出:https://ibb.co/hDZPgx
这是代码:

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

public class GUIJframe extends JFrame {

    private JButton jb1 = new JButton("first");
    private JButton jb2 = new JButton("second");
    private JButton jb3 = new JButton("third");
    private GridBagConstraints gbc = new GridBagConstraints();
    private GridBagLayout gbl = new GridBagLayout();

    public GUIJframe () {
        jcb.addItem(names);
        setLocation(150,150);
        setSize(500,500);
        setLayout(gbl);
        addComponent(jb1,0,0,1,1);
        addComponent(jb2,1,0,2,2);
        addComponent(jb3,2,2,5,5);
        setVisible(true);
    }

    public void addComponent (Component component, int gridx,int gridy, int width, int height) {
        gbc.gridx = gridx;
        gbc.gridy = gridy;
        gbc.gridwidth = width;
        gbc.gridheight = height;
        gbl.setConstraints(component,gbc);
        add (component);
    }
}

I expected that the second button height and width would be twice the size of the first button height and width

你的期望是错误的。

GridBagLayout 单元格是灵活的。一列的宽度不依赖于其他列的宽度。它仅取决于其中子组件的宽度(以及 insets 和 ipadx,如果已设置)。

如果一个组件占用两列,但其中任何一列都没有其他内容,则 GridBagLayout 没有理由将列扩展到比容纳该组件的首选宽度所需的更大的列。

高度和GridBagLayout行也是如此。

要强制一个组件的大小是另一个组件的两倍,您将需要不同的布局。 SpringLayout can do it, though it’s not easy to use. You might find it easier to simply add a ComponentListener on the “smaller” component, and use that component’s new size as the basis for the ”larger” component‘s preferred size.

为了达到您想要的效果,我会使用 Spring布局。

SpringLayout 允许您创建称为 Springs 的约束,它表示组件的边缘、宽度和高度的大小范围。这些可能取决于其他组件或容器边缘的尺寸范围。

SpringLayout layout = new SpringLayout();
setLayout(layout);

SpringLayout.Constraints jb1Constraints =
    new SpringLayout.Constraints(jb1);

SpringLayout.Constraints jb2Constraints =
    new SpringLayout.Constraints(
        Spring.sum(jb1Constraints.getX(), jb1Constraints.getWidth()),
        jb1Constraints.getY(),
        Spring.scale(jb1Constraints.getWidth(), 2),
        Spring.scale(jb1Constraints.getHeight(), 2));

SpringLayout.Constraints jb3Constraints =
    new SpringLayout.Constraints(
        Spring.sum(jb2Constraints.getX(),
                   Spring.scale(jb2Constraints.getWidth(), 0.5f)),
        Spring.sum(jb2Constraints.getY(), jb2Constraints.getHeight()),
        Spring.scale(jb1Constraints.getWidth(), 5),
        Spring.scale(jb1Constraints.getHeight(), 5));

add(jb1, jb1Constraints);
add(jb2, jb2Constraints);
add(jb3, jb3Constraints);

// Make container big enough to hold all components.

layout.putConstraint(
    SpringLayout.EAST, getContentPane(), 0,
    SpringLayout.EAST, jb3);

layout.putConstraint(
    SpringLayout.SOUTH, getContentPane(), 0,
    SpringLayout.SOUTH, jb3);

jb1 的约束是最简单的:遵守该组件的最小尺寸、首选尺寸和最大尺寸。位置是 (0, 0).

jb2 的约束取决于 jb1 的约束:

  • jb2的x坐标的Spring为jb1.x + jb1.width,实际上是jb1的右边缘。
  • jb2 与 jb1 具有相同的 y(实际上为零)。
  • 宽度和高度是jb1的宽度和高度,按2缩放。

jb3 的约束取决于 jb2 和 jb1:

  • x坐标为jb2.x + (jb2.width ÷ 2),即jb2的水平中心点。
  • y坐标为jb2.y + jb2.height,实际上是jb2的底边。
  • 宽度和高度是jb1的宽度和高度,按5缩放。

最后,SpringLayout 与其他布局管理器不同,因为它不会自动将其布局的容器大小设置为足以容纳所有子组件。我们必须自己完成这项工作,通过使用 SpringLayout.putConstraint 到 link 容器的右边缘(东)到 jb3 的右边缘,容器的底边缘(南)到 jb3 的底边缘。