gpt4 book ai didi

java - GridBagLayout 行为不当。水平分布问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:42 27 4
gpt4 key购买 nike

这就是我要实现的目标:

   |--0--|--1--|--2--|--3--|--4--|--5--|--6--|--7--|--8--|--9--|
0 | |JLabe|l----|-----|-----|-----|-----|-----|-----|-----|
1 | | |JLabe|l----|-----|-----|-----|-----|-----|-----|
2 | | | |JLabe|l----|-----|-----|-----|-----|-----|
3 | | | |JLabe|l----|-----|-----|-----|-----|-----|

这是我到目前为止所得到的,这是我所能达到的最接近的结果。

        center.setLayout(centerLayout);
JPanel[] card = new JPanel[1]; //update as it gets longer, eventually Scoreboard.LENGTH
card[0] = new JPanel();
GridBagLayout cardLayout = new GridBagLayout();
card[0].setLayout(cardLayout);
cardLayout.setConstraints(winner, new GridBagConstraints(0, 0, 10, 1, 1, 0, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
card[0].add(winner);
cardLayout.setConstraints(name, new GridBagConstraints(1, 1, 9, 1, 0.8, 0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
card[0].add(name);
cardLayout.setConstraints(with, new GridBagConstraints(2, 2, 8, 1, 0.7, 0, GridBagConstraints.LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
card[0].add(with);
cardLayout.setConstraints(score, new GridBagConstraints(2, 3, 8, 1, 0.7, 0, GridBagConstraints.LAST_LINE_START, GridBagConstraints.NONE, new Insets(0,0,0,0), 0, 0));
card[0].add(score);
center.add(card[0], "card1");
this.getContentPane().add(center);

出于某种原因,它显示如下:

   |--0--|--1--|--2--|--3--|--4--|--5--|--6--|--7--|--8--|--9--|
0 |JLabe|l----|-----|-----|-----|-----|-----|-----|-----|-----|
1 |JLabe|l----|-----|-----|-----|-----|-----|-----|-----|-----|
2 |JLabe|l----|-----|-----|-----|-----|-----|-----|-----|-----|
3 |JLabe|l----|-----|-----|-----|-----|-----|-----|-----|-----|

也许有一种比使用 GridBagLayout 更合适的方式来布置我的文本。 (它确实需要在不同的分辨率下工作,所以使用的布局管理器需要灵活)

最佳答案

1) 命名一个 GridBagLayout 变量“cardLayout”不是最好的主意,IMO。

2) 因为你只给了一个代码片段,没有提供 SSCCE ,我只能根据您提供的“图片”进行猜测。 GridBagLayout 需要在每一行/列中都有一个组件来定义它的大小。给定的列将与在该列中找到的具有最宽首选大小的组件一样宽。高度也一样。因此,根据您的结果图片,第 0 列中没有具有首选宽度的组件。一种解决方法是将 Box.createHorizo​​ntalStrut(someWidth) 放置在所需的列中。

关于java - GridBagLayout 行为不当。水平分布问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247662/

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