Java JPanel GridBagLayout未对齐垂直对象

Java JPanel GridBagLayout未对齐垂直对象,java,swing,jpanel,gridbaglayout,Java,Swing,Jpanel,Gridbaglayout,以下代码的问题在于,它将菜单页分成两半,而不是所需的10%到90% TitlePane和ControlPane只是jpanel public class MenuPane extends JPanel { public MenuPane( int width, int height ){ setSize( width, height ); setLayout( new GridBagLayout() ); GridBagConstraints c = new Gr

以下代码的问题在于,它将
菜单页
分成两半,而不是所需的10%到90%

TitlePane
ControlPane
只是jpanel

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 );

}

}
我真的不知道代码是做什么的,但是组件只有在组件显示在可见的GUI上时才有大小。因此,getWidth()/Height()方法将返回0

如果希望零部件以90/10的比例显示,则初始首选尺寸也必须以90/10的比例显示。权重因子仅在调整组件大小时适用

我不知道您的ControlPane代码是什么样子,因此我无法建议进行具体更改

您可以尝试使用专门为此设计的。当您将组件添加到容器中时,您只需要指定定量,它将从此处管理大小

add( new TitlePane( this.getWidth(), (int) (this.getHeight()*c.weighty) ), c );