在 Java 中使用 GroupLayout 构建 GUI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16148958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Building GUI using GroupLayout in Java
提问by newtothissite
I need to build a GUI using GroupLayout (not other layouts). The GUI will look like the following:
我需要使用 GroupLayout(而不是其他布局)构建 GUI。GUI 将如下所示:
----------------------------
| field 1 field 2 field 3 |
| FFIEEELLLDD4 FIELDDDDDD5 |
| FIEEEEEEEEEEEEEEELDDDD 6 |
_____________________________
Fields 1 - 3 take 1 length each, field 4 and 5 take 1.5 length each, and field 6 takes 3 length. The three groups are aligned both at the beginning and the end.
字段 1 - 3 各占用 1 长度,字段 4 和 5 各占用 1.5 长度,字段 6 占用 3 长度。这三个组在开头和结尾都对齐。
I've been referring to this http://docs.oracle.com/javase/tutorial/uiswing/layout/group.html.
我一直在指这个http://docs.oracle.com/javase/tutorial/uiswing/layout/group.html。
For simplicity, I will use JLabels as place holders for the fields.
为简单起见,我将使用 JLabels 作为字段的占位符。
Here's my code so far and I have no luck getting the GUI I wanted.
到目前为止,这是我的代码,我没有运气得到我想要的 GUI。
public class RecorderGUI extends JFrame {
private final JLabel one;
private final JLabel two;
private final JLabel three;
private final JLabel four;
private final JLabel five;
private final JLabel six;
public RecorderGUI() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GroupLayout layout = new GroupLayout(panel);
panel.setLayout(layout);
one = new JLabel("one");
two = new JLabel("two");
three = new JLabel("three");
four = new JLabel("four");
five = new JLabel("five");
six = new JLabel("six");
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup())
.addComponent(one)
.addComponent(two)
.addComponent(three)
.addGroup(layout.createSequentialGroup())
.addComponent(four)
.addComponent(five))
.addComponent(six));
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(one)
.addComponent(two)
.addComponent(three))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(four)
.addComponent(five))
.addComponent(six));
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(final String[] args) {
RecorderGUI GUI = new RecorderGUI();
}
The code is resulting in the following, which is not what I wanted: one, two and three are merged together; four and five overlap as well.
代码结果如下,这不是我想要的:一、二、三合并在一起;四和五也重叠。
Sorry, I would like to add a picture of the output GUI, but I can't attach pictures because I have under 10 reputation :(.
抱歉,我想添加输出 GUI 的图片,但我无法附加图片,因为我的声誉低于 10 :(。
回答by Howard
Fixed some parantheses and added resizing hints for components:
修复了一些括号并为组件添加了调整大小提示:
layout.setHorizontalGroup(layout
.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(one, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(two, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(three, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(four, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(five, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(six, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(one).addComponent(two).addComponent(three))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(four).addComponent(five))
.addComponent(six));