作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这就是我要实现的目标:
|--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.createHorizontalStrut(someWidth)
放置在所需的列中。
关于java - GridBagLayout 行为不当。水平分布问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247662/
我正在编写一个 native iOS 7 应用程序,需要从 JSON api(由我控制)检索数据。 JSON 输出示例如下: { "id" : "544", "name" : "1900 Green
我是一名优秀的程序员,十分优秀!