英文:
How to center a flowlayout vertically
问题
我正在尝试在另一个面板内垂直居中一个使用"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实现所需的行为?
英文:
I am trying to center a "Flowlayout"-ed JPanel vertically inside another Panel. Here is what I am trying to achieve:
-----------------
| |
| one two three |
| four five six |
| seven eight |
| nine |
| |
-----------------
-----------------------
| |
| one two three four |
| five six seven eight |
| nine |
| |
-----------------------
The content should flow, grow and shrink within the bounds of the containing panel and will also be centered vertically.
Unfortunatly all I could master was a top aligned panel which looks like:
-----------------------
| 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)));
}
}
Is there a way to achieve the desired behaviour with MigLayout?
答案1
得分: 3
我试图在另一个面板内垂直居中一个“Flowlayout”面板。
包装面板可以使用GridBagLayout
。
默认情况下,添加到面板的任何组件都将在垂直和水平方向上居中。因此,在使用FlowLayout
添加面板时,您还需要设置weightx
约束,这将允许面板在水平方向上增长。
英文:
> I am trying to center a "Flowlayout"-ed JPanel vertically inside another Panel
The wrapper panel can use a GridBagLayout
.
By default any component added to the panel will be centered both vertically and horizontally. So you will also need to set the weightx
constraint when you add the panel using the FlowLayout
which will allow the panel to grow horizontally.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论