如何垂直放置布局

时间:2020-07-27 17:04:57

标签: java swing

我正在尝试将“ Flowlayout”版的JPanel垂直居中放置在另一个面板中。这是我要实现的目标:

 -----------------
|                 |   
|  one two three  |
|  four five six  |
|  seven eight    |
|  nine           |
|                 |
 -----------------

 ----------------------- 
|                       |   
|  one two three four   |
|  five six seven eight |
|  nine                 |
|                       |
 -----------------------

内容应在包含面板的范围内流动,增长和收缩,并且也将垂直居中放置。

不幸的是,我只能掌握一个顶部对齐的面板,如下所示:

 ----------------------- 
|  one two three four   |
|  five six seven eight |
|  nine                 |
|                       |    
|                       |
 -----------------------
public class ListFlowCell extends JPanel
{

    public ListFlowCell(List<String> list)
    {
            
        setLayout(new FlowLayout(FlowLayout.LEADING));  
        list.forEach(s -> add(new JLabel(s)));
    
    }

}

是否可以通过MigLayout实现所需的行为?

1 个答案:

答案 0 :(得分:3)

我正在尝试将“ Flowlayout”版本的JPanel垂直居中放置在另一个面板中

包装器面板可以使用GridBagLayout

默认情况下,添加到面板的任何组件都将在垂直和水平方向居中。因此,在使用weightx添加面板时,还需要设置FlowLayout约束,这将允许面板水平增长。