简体   繁体   English

网格宽度和网格高度不起作用

[英]gridwidth and gridheight aren't working

i'm playing with GridBagLayout in java, I wrote the following code but it's not working as I expected 我正在使用Java中的GridBagLayout进行游戏,我编写了以下代码,但未按预期工作
I expected that the first button would be placed in point (0,0) which is upper left of the frame. 我希望第一个按钮将放置在框架左上方的点(0,0)上。
secondly, I expected that the second button height and width would be twice the size of the first button height and width but they weren't. 其次,我希望第二个按钮的高度和宽度是第一个按钮的高度和宽度的两倍,但不是。
here is the output: https://ibb.co/hDZPgx 这是输出: https : //ibb.co/hDZPgx
here is the code: 这是代码:

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 我希望第二个按钮的高度和宽度是第一个按钮的高度和宽度的两倍

Your expectation is wrong. 您的期望是错误的。

GridBagLayout cells are flexible. GridBagLayout单元很灵活。 The width of one column does not depend on the width of other columns. 一列的宽度不取决于其他列的宽度。 It only depends on the widths of child components in it (and the insets and ipadx, if set). 它仅取决于其中子组件的宽度(以及插图和ipadx(如果已设置))的宽度。

If a component takes up two columns, but there is nothing else in either of those columns, there is no reason for the GridBagLayout to expand the columns any larger than necessary to accommodate the preferred width of that component. 如果一个组件占用两列,但在这两列中都没有其他内容,则GridBagLayout没有理由将这些列扩大到超出容纳该组件的首选宽度所需的任何大小。

The same is true for heights and GridBagLayout rows. 高度和GridBagLayout行也是如此。

To force one component to be twice the size of another, you will need a different layout. 为了使一个组件的大小是另一个组件的两倍,您将需要不同的布局。 SpringLayout can do it, though it's not easy to use. 尽管它不容易使用,但SpringLayout可以做到。 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 . 您可能会发现,简单地在“较小” 的组件添加ComponentListener并使用该组件的新大小作为“较大”组件的首选大小的基础会更容易。

To achieve your desired result, I would use SpringLayout. 为了达到您想要的结果,我将使用SpringLayout。

SpringLayout lets you create constraints, called Spring s, which represent sizing ranges for edges, widths, and heights of components. SpringLayout使您可以创建约束(称为Spring) ,这些约束表示组件的边缘,宽度和高度的大小范围。 These can depend on the size ranges of other components, or edges of the container. 这些可能取决于其他组件的尺寸范围或容器的边缘。

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);

The constraints for jb1 are the easiest: honor that component's minimum size, prefered size, and maximum size. jb1的约束最简单:遵守组件的最小大小,首选大小和最大大小。 Location is (0, 0). 位置是(0,0)。

The constraints for jb2 depend on those of jb1 : 为约束jb2取决于那些jb1

  • The Spring for the x coordinate of jb2 is jb1.x + jb1.width, which is effectively the right edge of jb1. jb2的x坐标的jb2为jb1.x + jb1.width, jb2是jb1的右边缘。
  • jb2 has the same y as jb1 (which is, in fact, zero). jb2的y与jb1相同(实际上为零)。
  • The width and height are those of jb1, scaled by 2. 宽度和高度是jb1的宽度和高度,按2缩放。

The constraints for jb3 depend on both jb2 and jb1: jb3的约束取决于jb2和jb1:

  • The x coordinate is jb2.x + (jb2.width ÷ 2), that is, the horizontal center point of jb2. x坐标为jb2.x +(jb2.width÷2),即jb2的水平中心点。
  • The y coordinate is jb2.y + jb2.height, which is effectively the bottom edge of jb2. y坐标为jb2.y + jb2.height,实际上是jb2的底边。
  • The width and height are those of jb1, scaled by 5. 宽度和高度是jb1的宽度和高度,按5缩放。

Finally, SpringLayout is different from other layout managers, in that it doesn't automatically set the size of the container it's laying out to be big enough to hold all child components. 最后,SpringLayout与其他布局管理器的不同之处在于,它不会自动将其布局的容器的大小设置为足以容纳所有子组件的大小。 We have to do that work ourselves, by using SpringLayout.putConstraint to link the container's right edge (EAST) to the jb3's right edge, and the container's bottom edge (SOUTH) to jb3's bottom edge. 我们必须通过使用SpringLayout.putConstraint来将容器的右边缘(EAST)链接到jb3的右边缘,并将容器的底边缘(SOUTH)链接到jb3的下边缘来自己完成这项工作。

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

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