gpt4 book ai didi

java - 无法使 GridBagLayout 位置具有重量工作

转载 作者:行者123 更新时间:2023-12-01 12:01:06 26 4
gpt4 key购买 nike

我正在尝试制作一个 3x2 网格。在网格中我想要一张图片和一个相应的单选按钮。所以图片在 (0,0) 中,图片在 (1,0) 中。我无法让weightx 和weighty 系统工作并且所有组件都留在一条线上。我现在已经卸下了重物。代码如下所示:

    //the two numbers are corresponding with coordinates. For field00, x=0 and y=0
GridBagConstraints field00 = new GridBagConstraints();
field00.fill = GridBagConstraints.HORIZONTAL;
field00.gridx = 0;
field00.gridy = 0;
this.add(nokiaPic, field00);
GridBagConstraints field10 = new GridBagConstraints();
field10.fill = GridBagConstraints.HORIZONTAL;
field10.gridx = 1;
field10.gridy = 0;
this.add(nokiaButton, field10);

GridBagConstraints field01 = new GridBagConstraints();
field01.fill = GridBagConstraints.HORIZONTAL;
field01.gridx = 0;
this.add(princessPic, field01);
GridBagConstraints field11 = new GridBagConstraints();
field11.gridx = 1;
field11.gridy = 1;
this.add(princessButton, field11);

To give you an idea:
+--------+--------+
| | |
| pic | Button |
| | |
+--------+--------+
| | |
| pic | Button |
| | |
+--------+--------+
| | |
| pic | Button |
| | |
+--------+--------+

我知道我没有使用 GridBagConstraints 的正常约定,但我不明白为什么这个方法不能很好地工作。我希望你能帮我添加权重,使图片看起来像在左列中,而相应的单选按钮在右行中。

最佳答案

在您的情况下,您可以简单地使用GridLayout,但如果您仍然想使用GridBagLayout,则使用单个GridBagConstraints:

    GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
this.add(nokiaPic, gbc);
gbc.gridx = 1;
this.add(nokiaButton, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
this.add(princessPic, gbc);
gbc.gridx = 1;
this.add(princessButton, gbc);

关于java - 无法使 GridBagLayout 位置具有重量工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27976784/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com