gpt4 book ai didi

java - 带按钮的 GridBagLayout

转载 作者:行者123 更新时间:2023-11-30 07:50:12 25 4
gpt4 key购买 nike

我正在尝试绘制一个带有按钮的面板,看起来像这样:

enter image description here

我这样创建“buttonPanel”:

buttonPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0;
gbc.weighty = 0;

gbc.gridx = 0; gbc.gridy = 0;
gbc.gridwidth = 1; gbc.gridheight = 2;
buttonPanel.add(new JButton("A0"), gbc);

gbc.gridx = 1; gbc.gridy = 0;
gbc.gridwidth = 1; gbc.gridheight = 2;
buttonPanel.add(new JButton("A1"), gbc);

gbc.gridx = 2; gbc.gridy = 0;
gbc.gridwidth = 2; gbc.gridheight = 2;
buttonPanel.add(new JButton("A2"), gbc);

gbc.gridx = 0; gbc.gridy = 2;
gbc.gridwidth = 3; gbc.gridheight = 2;
buttonPanel.add(new JButton("A3"), gbc);

gbc.gridx = 3; gbc.gridy = 2;
gbc.gridwidth = 1; gbc.gridheight = 2;
buttonPanel.add(new JButton("A4"), gbc);

但是结果是这样的:

enter image description here

由于按钮“A3”的网格宽度=3,它应该到达按钮“A2”的中间吗?

有什么帮助吗?

编辑1:增加面板大小或在按钮上设置更长的文本不会改变任何内容:

enter image description here

编辑2:为按钮“A2”和“A3”设置 weightx=1 有助于:

enter image description here

这对我来说是可以接受的,尽管如果按钮“A4”的宽度与按钮“A0”和“A1”的宽度相同就更好了

最佳答案

将 A2 和 A3 的 weightx 设置为 1.0。

关于java - 带按钮的 GridBagLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47953536/

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