[英]GridBagLayout elements not moving
I'm trying to move my buttons and label. 我正在尝试移动按钮和标签。 I want the buttons down in the bottom right corner and the label right above them, but no matter how I change the layout system, they're all stuck on the same line and won't move at all. 我希望按钮位于右下角,标签位于其右上方,但是无论我如何更改布局系统,它们都卡在同一行上并且根本不会移动。 How to get them to move? 如何让他们移动?
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
gc.anchor = GridBagConstraints.CENTER;
gc.weighty = 1;
gc.gridx = 0;
gc.gridy = 0;
gc.ipadx = 5;
pane.add(explain_lbl, gc);
gc.ipadx = 1;
gc.gridy = 1;
gc.anchor = GridBagConstraints.PAGE_END; //bottom of space
pane.add(btn_play_normal, gc);
gc.ipadx = 6;
gc.gridy = 1;;
gc.anchor = GridBagConstraints.PAGE_END; //bottom of space
gc.fill = GridBagConstraints.HORIZONTAL;
gc.ipady = 0; //reset to default
gc.weighty = 1.0; //request any extra vertical space
gc.anchor = GridBagConstraints.PAGE_END; //bottom of space
gc.insets = new Insets(10,0,0,0); //top padding
gc.gridx = 1; //aligned with button 2
gc.gridwidth = 2; //2 columns wide
gc.gridy = 2; //third row
pane.add(btn_play_ptr, gc);
You are not setting the layout of pane
the container that is accepting your GridBag components. 您没有在设置接受GridBag组件的容器的pane
布局。 You need to set its layout to GridBagLayout for the layout to work. 您需要将其布局设置为GridBagLayout,布局才能正常工作。
Specifically: 特别:
setLayout(new GridBagLayout()); // (A)
// ....
pane.add(btn_play_normal, gc); // (B)
// ...
pane.add(btn_play_ptr, gc); // (C)
You're adding your components to pane using GridBagConstraints, gc, in (B) and (C), but you appear to be setting the layout of the this
container (whatever it is) in (A), but not that of the pane
container. 您正在使用(B)和(C)中的GridBagConstraints gc将组件添加到窗格中,但似乎在(A)中设置了this
容器的布局(无论它是什么),但未设置pane
的布局容器。
A solution: set pane's layout: 解决方案:设置窗格的布局:
pane.setLayout(new GridBagLayout());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.