[英]JPanel GridBagLayout not aligning vertical objects
以下代码的问题在于它将MenuPane
分成两半而不是所需的10%到90%
TitlePane
和ControlPane
只是JPanels。
public class MenuPane extends JPanel {
public MenuPane( int width, int height ){
setSize( width, height );
setLayout( new GridBagLayout() );
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.0d;
c.weighty = 0.1d;
add( new TitlePane( this.getWidth(), (int) (this.getHeight()*c.weighty) ), c );
c.gridx = 0;
c.gridy = 1;
c.weightx = 0.0d;
c.weighty = 0.9d;
add( new ControlPane( this.getWidth(), (int) (this.getHeight()*c.weighty) ), c );
}
}
add( new TitlePane( this.getWidth(), (int) (this.getHeight()*c.weighty) ), c );
不知道该代码的作用,但组件在可见GUI上显示组件之前没有大小。 所以你的getWidth()/ Height()方法将返回0。
如果您希望组件以90/10比率显示,则初始首选大小也必须为90/10比率。 权重因子仅适用于调整组件大小的情况。
我不知道你的ControlPane代码是什么样的,所以我不能建议做出具体的改变。
您可以尝试使用专门为此设计的相对布局 。 您只需在将组件添加到容器时指定配给量,它就会从那里管理大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.