让 BoxLayout 将组件移动到顶部,同时从左到右堆叠
我有一个 JPanel
,它在 X_AXIS
方向使用 BoxLayout
。我遇到的问题最好用一张图片来展示:
正如您所看到的,左侧的 JPanel
已居中而不是顶部对齐。我希望它们都在顶部对齐并从左到右堆叠,如何使用此布局管理器实现此目的?我写的代码如下:
public GameSelectionPanel(){
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setAlignmentY(TOP_ALIGNMENT);
setBorder(BorderFactory.createLineBorder(Color.black));
JPanel botSelectionPanel = new JPanel();
botSelectionPanel.setLayout(new BoxLayout(botSelectionPanel, BoxLayout.Y_AXIS));
botSelectionPanel.setBorder(BorderFactory.createLineBorder(Color.red));
JLabel command = new JLabel("Please select your opponent:");
ButtonGroup group = new ButtonGroup();
JRadioButton button1 = new JRadioButton("hello world");
JRadioButton button2 = new JRadioButton("hello world");
JRadioButton button3 = new JRadioButton("hello world");
JRadioButton button4 = new JRadioButton("hello world");
group.add(button1);
group.add(button2);
group.add(button3);
group.add(button4);
botSelectionPanel.add(command);
botSelectionPanel.add(button1);
botSelectionPanel.add(button2);
botSelectionPanel.add(button3);
botSelectionPanel.add(button4);
JPanel blindSelectionPanel = new JPanel();
blindSelectionPanel.setBorder(BorderFactory.createLineBorder(Color.yellow));
blindSelectionPanel.setLayout(new BoxLayout(blindSelectionPanel, BoxLayout.Y_AXIS));
JRadioButton button5 = new JRadioButton("hello world");
JRadioButton button6 = new JRadioButton("hello world");
ButtonGroup group2 = new ButtonGroup();
group2.add(button5);
group2.add(button6);
JLabel blindStructureQuestion = new JLabel("Please select the blind structure:");
blindSelectionPanel.add(blindStructureQuestion);
blindSelectionPanel.add(button5);
blindSelectionPanel.add(button6);
add(botSelectionPanel);
add(blindSelectionPanel);
setVisible(true);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Riduidel 关于在
GameSelectionPanel
本身上设置setAlignmentY
的说法是正确的,并且GridBagLayout 是一个很好的替代方案。如果您更喜欢使用BoxLayout
,请参阅文章 修复对齐问题讨论了这个问题,建议“由从左到右的 Boxlayout 控制的所有组件通常应该具有相同的 Y 对齐方式。”在您的示例中,添加Riduidel is correct about setting
setAlignmentY
on theGameSelectionPanel
itself, andGridBagLayout
is an excellent alternative. If you prefer to stick withBoxLayout
, the article Fixing Alignment Problems discusses the matter, suggesting "all the components controlled by a left-to-right Boxlayout should generally have the same Y alignment." In your example, add好吧,setAlignmentY 方法在这里不起作用,因为它作用于被视为组件的面板。
正如您所猜测的,包含的面板的布局是由您使用的布局管理器定义的。
不幸的是,BoxLayout 不提供您正在查看的功能。
在标准 JDK 中,显然,您的问题选择的布局是 GridBagLayout。虽然一开始很难理解,但它会很快向您展示其在组件排列方面的强大功能。
使用有用的 GBC 类,您的组件可以这样排列:
或者我认为是这样;-)
Well, the
setAlignmentY
method has no effect here, since it acts on the panel considered as a component.As you have guessed, the layout of contained panels is defined by the layout manager you use.
Unfortunatly,
BoxLayout
do not provide the kind of feature you're looking at.in standard JDK, obviously, the layout of choice for your problem is
GridBagLayout
. Although rather hard to understand at first, it will fast reveal to you its power in components arrangement.using the useful GBC class, your components could be arranged as such :
or I think so ;-)