简体   繁体   English

Java Swing MigLayout,以跨越行的两个元素为中心

[英]Java Swing MigLayout, centering two elements inside a spanned row

I'm working on a MigLayout form with 3 columns and 4 rows, like this: 我正在处理一个包含3列和4行的MigLayout表单,如下所示:

"wrap 3",
"[15%] 15px [45%] 15px [40%]",
"20 [] 15 [] 15 [grow,fill] 15 []"

Now my goal is to have it look like this: 现在我的目标是让它看起来像这样:

.------------------------------------.
| 15% |     45%      |     40%       |
|------------------------------------|
|     |              |               |
|------------------------------------|
|     |              |               |
|------------------------------------|
|           button,button            |
`------------------------------------´

I want the buttons on the last row centered, so I assumed that it first requires that I span the 3 columns of the 4th row into one with the "span 3, center" component constraint on the button. 我希望最后一行的按钮居中,所以我假设它首先要求我将第4行的3列"span 3, center"到按钮上的"span 3, center"组件约束。

This works nicely with just one button, but I'm having problems figuring out how to add the second button, while keeping both two buttons centered on the same row at the same time. 只需一个按钮就可以很好地工作,但是我在确定如何添加第二个按钮时遇到了问题,同时保持两个按钮同时位于同一行的中心。 If i add same constraints on the second button it appears perfectly centered below the first button on the next row. 如果我在第二个按钮上添加相同的约束,它会在下一行的第一个按钮下方完美居中。

The solution is to span and split at the same time: the split defines the number of components that should live in the spanned cell: 解决方案是同时跨越拆分:拆分定义应该在跨区单元中存在的组件数量:

panel.add(firstButton, "span, split 2, center");
panel.add(secondButton);

Aside: a span without count defaults to a high number that it effectly means "all" 除此之外:没有计数的跨度默认为高数字,它实际上意味着“全部”

It's not ideal, but you could add the two buttons to a new JPanel, and then nest that JPanel inside your existing layout with the "span 3, center" 这不是理想的,但您可以将两个按钮添加到新的JPanel,然后将JPanel嵌套在现有布局中,并使用“span 3,center”

I'm struggling to think of another way. 我正在努力想到另一种方式。

you could try to put two boxes on the right and left sides that will grow or push the buttons in middle like this: 您可以尝试在左右两侧放置两个盒子,这样可以增长或按下中间的按钮,如下所示:

pane.setLayout(new MigLayout("fill"));
pane.add(Box.createHorizontalBox(), "push");
pane.add(new JButton("asdf"));
pane.add(new JButton("zxcv"));
pane.add(Box.createHorizontalBox(), "push,wrap");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM