Java,使用GridBagLayout布置面板时,在面板上添加按钮会更改大小,如何防止这种情况发生? [英] Java, When using GridBagLayout to layout panels, adding buttons to the panels changes the sizes, how do I prevent this?

查看:146
本文介绍了Java,使用GridBagLayout布置面板时,在面板上添加按钮会更改大小,如何防止这种情况发生?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,所以我在弄乱GridBagLayout来掌握它.我想用它来布局JFrame中的JPanel,但是这些单独的面板将使用不同的布局管理器,GridBagLayout可能不适合.

Ok so I am messing around with GridBagLayout to get to grips with it. I want to use it to layout JPanels in a JFrame but these individual panels will use different layout managers, GridBagLayout probably won't suit.

但是我有一个问题.如果我没有在面板上添加任何内容,则所有比例都可以完美显示,如以下示例所示:

But i have a problem. If i don't add anything to the panels, all of the proportions work out perfectly as in this example:

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,但我不想错过任何事情.

I know that I redefined some GridBagConstraints unnecessarily but I didn't want to miss anything.

无论如何,当我在任何面板上添加按钮时,都会出现我的问题.像这样:

Anyway, my problem arises when i add a button to any of the panels. Like this:

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面板变大,因此与之相关的网格正方形的大小.

The bottomLeft panel got bigger and hence the size of the grid squares related to it.

在第一个示例中,一切都占据了窗口宽度或高度的百分比,该百分比由以下因素决定:

In the first example, everything is taking up a percentage of the width or height of the window dictated by:

gbc.weightx

还有

gbc.weighty

通过这种方式,可以很容易地将所有内容设置为正确的大小.

Doing it this way makes it very easy to make everything the correct size.

有没有一种解决方法可以让我强制面板大小仅受重量值影响?

Is there a workaround which allows me to force the panel sizes to be influenced only by the weight values?

推荐答案

weightx和weighty显示基础组件未请求的空间比例分配.例如,如果您有一个标签和一个水平放置的文本字段,则您可能希望标签根据列中最大的标签采取固定的间距,其余的空间则移至文本字段.将所有weightx分配给文本字段,这就是实际发生的情况.标签的权重x为零,但不会缩小为零大小,因为只有多余的空间由权重决定.空面板的最小尺寸为零,因此所有空间都被认为是多余的.

weightx and weighty show the proportional assignment for space not requested by the underlying components. For example, if you have a label and a textfield oriented horizontally, you probably want the label to take a fixed amount of spaced based on the biggest label in the column, and the remainder of the space to go to the textfield. Assign all the weightx to the textfield and that is exactly what happens. The label has a weightx of zero, but is not scrunched to zero size because only the excess space is dictated by weight. Empty panels have a minimum size of zero, so all the space is considered extra.

如果要强制布局按照您描述的那样工作,则需要扩展JPanel,以使子面板位于GridBagLayout上以了解其内容.如果您开始制作小于最小尺寸的东西,这可能会给您带来麻烦.

If you want to force the layout to work as you describe, you would need to extend JPanel for the sub-panels to lie to the GridBagLayout about their contents. This could get you into trouble if you start making things smaller than their minimums.

这篇关于Java,使用GridBagLayout布置面板时,在面板上添加按钮会更改大小,如何防止这种情况发生?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆