2012-01-16 87 views
3

是否可以在Gridlayout verticaly中添加组件?我的意思是上一个之上的下一个?Java Swing Gridlayout垂直约束

谢谢

+2

您应该接受更多答案。您已经提出了55个问题,但其中不到一半的人接受了答案。 – Paul 2012-01-16 04:39:11

回答

3

没有,没有布局,可以让你从下向上垂直堆叠,至少我所知道的。如果你想要垂直堆叠,你可以使用带有单列的GridLayout或带纵轴的BoxLayout。通过嵌套面板和组合布局,您应该可以轻松获得所需的内容,例如具有垂直布局的面板全部布置在水平容器中。

+0

['List']的好用例(http://docs.oracle.com/javase/7/docs/api/java/util/List.html)或['Deque'](http:// docs .oracle.com/javase/7/docs/api/java/util/Deque.html)的实现。 – trashgod 2012-01-16 05:07:42

1

虽然这个答案与没有关系,但是网格布局,我强烈推荐使用JGoodies表单布局。它非常灵活。 http://www.jgoodies.com/freeware/forms/index.html

      /* 1     2  3  4 5     6  7  8  9*/  
      String col1 = "left:max(20dlu;pref), 3dlu, 70dlu, 15dlu,left:max(10dlu;pref),10dlu, 70dlu, 70dlu, 70dlu"; 
          /* 1 2  3 4 5 6 7 8 */ 
      String row1 = " p, 5dlu, p, 3dlu, p, 5dlu, p, 9dlu, "; 
      FormLayout layout = new FormLayout(col1, row1 + row2 + row3 + row4 + row5 + row6); 

      JPanel panel = new JPanel(layout); 
      panel.setBorder(Borders.DIALOG_BORDER); 

      // Fill the table with labels and components. 
      CellConstraints cc = new CellConstraints(); 
      panel.add(createSeparator("Registration Information"), cc.xyw(1, 1, 7)); 
      panel.add(new JLabel("Patient ID"), cc.xy(1, 3)); 
      panel.add(pid, cc.xyw(3, 3, 1)); 
      panel.add(new JLabel("Date and Time"), cc.xy(5, 3)); 

可以手代码来绘制每个部件的任何在所定义的布局设计即WRT其中对col和行。 即使是垂直排列。 阅读白皮书:http://www.jgoodies.com/articles/forms.pdf

3

布局一样从上到下的BoxLayout和网格布局显示组件当你使用:

panel.add(someComponent); 

,但你可以随时使用:

panel.add(someComponent, 0); 

在插入零部件最佳。