Java中,当使用的GridBagLayout布局面板,将按钮添加到面板改变大小,我该如何避免这种情

2019-09-28 12:58发布

好了,所以我瞎搞使用GridBagLayout的去处理它。 我想用它来布置在一个JFrame但这些单独的面板将使用不同的布局管理器JPanels,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);
        }
    });

}

}

该BOTTOMLEFT面板变得更大,因此与其相关的方格的大小。

在第一示例中,一切都占用由所规定的窗口的宽度或高度的百分比:

gbc.weightx

gbc.weighty

这样做,这样使得它很容易使一切正确的尺寸。

有一种解决方法,让我来迫使面板尺寸要由权重值仅影响?

Answer 1:

的weightx和沉重的表演不被底层组件所需的空间的比例分配。 举例来说,如果你有一个标签和水平方向的一个文本框,你可能希望标签来利用基于列的最大标签上的间隔一个固定的量,的剩余空间去文本框。 分配所有的weightx的文本框而这正是发生了什么。 该标签具有零的weightx,但因为只有多余的空间是由重量决定不揉成一团零大小。 空面板具有零的最小尺寸,因此,所有的空间被认为是多余的。

如果要强制布局象你所说的工作,你需要扩展JPanel的子板骗他们的内容的GridBagLayout。 这可能让你陷入麻烦,如果你开始做的事情比自己小的最小值。



文章来源: Java, When using GridBagLayout to layout panels, adding buttons to the panels changes the sizes, how do I prevent this?