如何在垂直方向上将流式布局居中。

huangapple go评论56阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年7月28日 01:04:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63120081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定