问题描述
我需要使用GroupLayout(而不是其他布局)构建GUI。 GUI将如下所示:
I need to build a GUI using GroupLayout (not other layouts). The GUI will look like the following:
----------------------------
| field 1 field 2 field 3 |
| FFIEEELLLDD4 FIELDDDDDD5 |
| FIEEEEEEEEEEEEEEELDDDD 6 |
_____________________________
字段1 - 3各取1个长度,字段4和5取1.5长度每个,而字段6需要3个长度。这三组在开始和结束时都是一致的。
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.
我一直在指这个。
为简单起见,我将使用JLabel作为字段的占位符。
For simplicity, I will use JLabels as place holders for the fields.
这是我的代码到目前为止我没有运气我想要的GUI。
Here's my code so far and I have no luck getting the GUI I wanted.
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.
对不起,我想添加输出GUI的图片,但我无法附上图片,因为我有10个以下的声誉:(。
Sorry, I would like to add a picture of the output GUI, but I can't attach pictures because I have under 10 reputation :(.
推荐答案
修正了一些parantheses并添加了组件的调整大小提示:
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));
这篇关于使用Java中的GroupLayout构建GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!