2013-02-13 78 views
0

我刚开始搞乱BoxLayout经理。BoxLayout坚持顶部

我做了两个彼此相邻的按钮,第三个应该到下一行(在前两个下面),并且两个第一个按钮应该在框架的顶部。

我怎样才能做到这一点?

这是我当前的代码

Box box = Box.createHorizontalBox(); 
    box.add(Box.createHorizontalGlue()); 
    box.add(new JButton("Button")); 
    box.add(new JButton("Hello")); 


    box.add(Box.createVerticalBox()); 
    box.add(Box.createVerticalStrut(100)); 
    box.add(new JButton("Button2")); 

    add(box); 

enter image description here

回答

2

您当前的代码看起来一点都不像你给你想要什么的描述。这听起来像你需要

  • 顶级立式机箱
    • 水平箱
      • 按钮
      • 差距
      • 按钮
    • 差距
    • 按钮

因此,像

Box vbox = Box.createVerticalBox(); 

Box hbox = Box.createHorizontalBox(); 
hbox.add(new JButton("Button")); 
hbox.add(Box.createHorizontalStrut(10)); 
hbox.add(new JButton("Hello")); 
vbox.add(hbox); 

vbox.add(Box.createVerticalStrut(100)); 
vbox.add(new JButton("Button2"));