使用Java中的GroupLayout构建GUI

时间:2013-04-22 13:55:23

标签: java swing grouplayout

我需要使用GroupLayout(而不是其他布局)构建GUI。 GUI将如下所示:

----------------------------
| field 1  field 2  field 3 |

| FFIEEELLLDD4 FIELDDDDDD5  |

| FIEEEEEEEEEEEEEEELDDDD 6  |
_____________________________

字段1 - 3各占1个长度,字段4和5各取1.5长度,字段6取3长度。这三组在开始和结束时都是一致的。

我指的是http://docs.oracle.com/javase/tutorial/uiswing/layout/group.html

为简单起见,我将使用JLabel作为字段的占位符。

到目前为止,这是我的代码,我没有运气获得我想要的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();
    }

代码导致以下内容,这不是我想要的: 一,二,三合并在一起;四和五重叠。

抱歉,我想添加输出图形用户界面的图片,但我无法附加图片,因为我的声誉不到10 :(。

1 个答案:

答案 0 :(得分:10)

修正了一些parantheses并添加了组件的调整大小提示:

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));

enter image description here