Java Swing MigLayout,将两个元素集中在跨行内
我正在开发一个 3 列 4 行的 MigLayout 表单,如下所示:
"wrap 3",
"[15%] 15px [45%] 15px [40%]",
"20 [] 15 [] 15 [grow,fill] 15 []"
现在我的目标是让它看起来像这样:
.------------------------------------.
| 15% | 45% | 40% |
|------------------------------------|
| | | |
|------------------------------------|
| | | |
|------------------------------------|
| button,button |
`------------------------------------´
我希望最后一行上的按钮居中,所以我假设它首先要求我跨越第 4 行的 3 列通过按钮上的“span 3, center”组件约束合并为一列。
仅使用一个按钮就可以很好地工作,但我在弄清楚如何添加第二个按钮,同时保持两个按钮同时位于同一行的中心时遇到问题。如果我在第二个按钮上添加相同的约束,它将完美地位于下一行第一个按钮下方的中心。
I'm working on a MigLayout form with 3 columns and 4 rows, like this:
"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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
解决方案是同时进行跨度和分割:分割定义了跨度单元格中应存在的组件数量:
旁白:没有计数的跨度默认为一个较高的数字,它实际上意味着“全部”
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:
Aside: a span without count defaults to a high number that it effectly means "all"
这并不理想,但您可以将这两个按钮添加到新的 JPanel 中,然后使用“跨度 3,中心”将该 JPanel 嵌套在现有布局中,
我正在努力想出另一种方法。
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"
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: