+--------------------------------------------+
| +-------+ +----------+|
| | +---+ | | +-----+ ||
| | | A | | | | B | ||
| | +---+ | | +-----+ ||
| +-------+ +----------+|
+--------------------------------------------+
^
|
|
Center
背景:我有
- 一个大小为50x25的JButton(“A”),在JPanel(FlowLayout.CENTER)中
- 一个大小为100x25的JLabel(“B”),在JPanel(FlowLayout.RIGHT)中
- 这两个JPanel都在一个JPanel中
期望结果:我希望
- JButton “A”始终水平居中,
- JLabel “B”始终右对齐。
我尝试过的方法:但它们对我没有用
- BorderLayout is not working for me because JButton "A" is shifted LEFT:
I'd prefer not to put an invisible component WEST to undo the shift
+--------------------------------------------+ | +-------+ +----------+| | | +---+ | | +-----+ || | | | A | | | | B | || | | +---+ | | +-----+ || | +-------+ +----------+| +--------------------------------------------+ ^ ^ | | | | | Center | Shifted Left
GridLayout won't work because I don't want the "A" and "B" to be expanded
附言:
JButton/JLabel各自位于自己的JPanel中,因为如果没有JPanel,BorderLayout.CENTER会将JButton扩展到整个主面板的宽度(直到JLabel的左边缘)。JPanels对问题陈述不是必需/关键。
结论:
我选择了下面发布的“满载鳗鱼的气垫船”的答案。谢谢!