Java,当使用GridBagLayout来布局面板时,向面板添加按钮会改变大小,如何防止这种情况?

Java,当使用GridBagLayout来布局面板时,向面板添加按钮会改变大小,如何防止这种情况?,java,swing,layout,jpanel,gridbaglayout,Java,Swing,Layout,Jpanel,Gridbaglayout,好吧,我正在摆弄GridBagLayout来处理它。我想用它在JFrame中布局JPanel,但这些单独的面板将使用不同的布局管理器,GridBagLayout可能不适合 但我有个问题。如果我不在面板上添加任何内容,则所有比例都会完美匹配,如本例所示: public class RealMess extends JFrame{ private JPanel panel; public RealMess(){ initUI(); } public final void initUI(

好吧,我正在摆弄GridBagLayout来处理它。我想用它在JFrame中布局JPanel,但这些单独的面板将使用不同的布局管理器,GridBagLayout可能不适合

但我有个问题。如果我不在面板上添加任何内容,则所有比例都会完美匹配,如本例所示:

public class RealMess extends JFrame{

private JPanel panel;

public RealMess(){
    initUI();
}

public final void initUI(){

    Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
    int width = (int) screensize.getWidth();
    int height = (int) screensize.getHeight();

    panel = new JPanel();
    panel.setLayout(new GridBagLayout());



    JPanel left = new JPanel();
    left.setBackground(Color.GREEN);

    JPanel centre = new JPanel();
    centre.setBackground(Color.PINK);

    JPanel right = new JPanel();
    right.setBackground(Color.CYAN);

    JPanel leftBottom = new JPanel();
    leftBottom.setBackground(Color.LIGHT_GRAY);
    leftBottom.setLayout(new FlowLayout(0));



    JPanel bottom = new JPanel();
    bottom.setBackground(Color.MAGENTA);

    GridBagConstraints gbc = new GridBagConstraints();

    gbc.weightx = 0.11;
    gbc.weighty = 0.9;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;

    panel.add(left, gbc);

    gbc.weightx = 0.11;
    gbc.weighty = 0.1;
    gbc.anchor = GridBagConstraints.LAST_LINE_START;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.BOTH;

    panel.add(leftBottom, gbc);

    gbc.weightx = 0.78;
    gbc.weighty = 0.9;
    gbc.anchor = GridBagConstraints.PAGE_START;
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;

    panel.add(centre, gbc);

    gbc.weightx = 0.78;
    gbc.weighty = 0.1;
    gbc.anchor = GridBagConstraints.PAGE_END;
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.BOTH;

    panel.add(bottom, gbc);

    gbc.weightx = 0.11;
    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.FIRST_LINE_END;
    gbc.gridx = 2;
    gbc.gridy = 0;
    gbc.gridheight = 2;
    gbc.fill = GridBagConstraints.BOTH;

    panel.add(right, gbc);

    add(panel);

    setTitle("Jack");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setExtendedState(MAXIMIZED_BOTH);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            RealMess rm = new RealMess();
            rm.setVisible(true);
        }
    });

}

}
我知道我不必要地重新定义了一些GridBagConstraints,但我不想错过任何东西

无论如何,当我向任何面板添加按钮时,我的问题就会出现。像这样:

public class RealMess extends JFrame{

private JPanel panel;

public RealMess(){
    initUI();
}

public final void initUI(){

    Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
    int width = (int) screensize.getWidth();
    int height = (int) screensize.getHeight();

    panel = new JPanel();
    panel.setLayout(new GridBagLayout());

    JPanel left = new JPanel();
    left.setBackground(Color.GREEN);

    JPanel centre = new JPanel();
    centre.setBackground(Color.PINK);

    JPanel right = new JPanel();
    right.setBackground(Color.CYAN);

    JPanel leftBottom = new JPanel();
    leftBottom.setBackground(Color.LIGHT_GRAY);
    leftBottom.setLayout(new FlowLayout(0));

    JButton rewind = new JButton("1");      
    rewind.setPreferredSize(new Dimension(50, 25));

    leftBottom.add(rewind);

    JPanel bottom = new JPanel();
    bottom.setBackground(Color.MAGENTA);

    GridBagConstraints gbc = new GridBagConstraints();

    gbc.weightx = 0.11;
    gbc.weighty = 0.9;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;

    panel.add(left, gbc);

    gbc.weightx = 0.11;
    gbc.weighty = 0.1;
    gbc.anchor = GridBagConstraints.LAST_LINE_START;
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.BOTH;

    panel.add(leftBottom, gbc);

    gbc.weightx = 0.78;
    gbc.weighty = 0.9;
    gbc.anchor = GridBagConstraints.PAGE_START;
    gbc.gridx = 1;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;

    panel.add(centre, gbc);

    gbc.weightx = 0.78;
    gbc.weighty = 0.1;
    gbc.anchor = GridBagConstraints.PAGE_END;
    gbc.gridx = 1;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.BOTH;

    panel.add(bottom, gbc);

    gbc.weightx = 0.11;
    gbc.weighty = 1;
    gbc.anchor = GridBagConstraints.FIRST_LINE_END;
    gbc.gridx = 2;
    gbc.gridy = 0;
    gbc.gridheight = 2;
    gbc.fill = GridBagConstraints.BOTH;

    panel.add(right, gbc);

    add(panel);

    setTitle("Jack");
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setExtendedState(MAXIMIZED_BOTH);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable(){
        public void run(){
            RealMess rm = new RealMess();
            rm.setVisible(true);
        }
    });

}

}
左下角的面板变大了,因此与之相关的网格正方形的大小也变大了

在第一个示例中,所有内容都占窗口宽度或高度的百分比,由以下内容决定:

gbc.weightx

这样做可以很容易地使所有的东西都达到正确的尺寸


是否有一种变通方法允许我强制面板尺寸仅受重量值的影响?

weightx和weighty显示底层组件未请求的空间比例分配。例如,如果有一个标签和一个水平方向的文本字段,则可能希望标签根据列中最大的标签采用固定的间距,剩余的空间用于文本字段。将所有权重x分配给textfield,这正是发生的情况。标签的权重X为零,但不会压缩到零大小,因为只有多余的空间由重量决定。空面板的最小尺寸为零,因此所有空间都被视为额外空间


如果您想强制布局按照您所描述的那样工作,您需要扩展JPanel,使子面板与GridBagLayout保持一致,以了解其内容。如果您开始使事物小于其最小值,这可能会给您带来麻烦。

weightx和weighty显示了底层组件未请求的空间的比例分配。例如,如果有一个标签和一个水平方向的文本字段,则可能希望标签根据列中最大的标签采用固定的间距,剩余的空间用于文本字段。将所有权重x分配给textfield,这正是发生的情况。标签的权重X为零,但不会压缩到零大小,因为只有多余的空间由重量决定。空面板的最小尺寸为零,因此所有空间都被视为额外空间


如果您想强制布局按照您所描述的那样工作,您需要扩展JPanel,使子面板与GridBagLayout保持一致,以了解其内容。如果你开始把东西做得比最小值小,这可能会给你带来麻烦。

好的,谢谢。我现在明白重量的目的了。因此,现实地说,为了得到正确的尺寸,我将不得不到处玩,直到它是我想要的。我说的对吗?好的,谢谢。我现在明白重量的目的了。因此,现实地说,为了得到正确的尺寸,我将不得不到处玩,直到它是我想要的。我这样说对吗?
gbc.weighty