GridBagLayout固定空间按权重x/y设置 [英] GridBagLayout fix space set by weight x/y

查看:98
本文介绍了GridBagLayout固定空间按权重x/y设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下3个JPanel布局的框架.

I have a frame with the following layout of 3 JPanels.

----------------------------
|   |                      |
| l |                      |
| e |  //toppanel          |
| f |                      |   
| t |----------------------|
| p |                      |
| n |  //bottompanel       |
| l |                      |
|   |                      |
----------------------------

我通过使用GridBagLayout

这是该布局的代码

public class UITests extends JFrame{
    public UITests(){
        setLayout(new GridBagLayout());

        //the three main panels
        JPanel leftPanel = new JPanel();
        JPanel topPanel = new JPanel();
        JPanel bottomPanel = new JPanel();

        leftPanel.setBackground(Color.YELLOW);
        topPanel.setBackground(Color.GREEN);
        bottomPanel.setBackground(Color.PINK);

        //position the three main panels
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 2;
        gbc.gridwidth = 1;
        gbc.fill = gbc.BOTH;
        gbc.weightx = .2;
        gbc.weighty = 1;
        getContentPane().add(leftPanel, gbc);

        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.fill = gbc.BOTH;
        gbc.weightx = .8;
        gbc.weighty = .5;
        getContentPane().add(topPanel, gbc);

        gbc = new GridBagConstraints();
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.fill = gbc.BOTH;
        gbc.weightx = .8;
        gbc.weighty = .5;
        getContentPane().add(bottomPanel, gbc);


        setSize(600,600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
    public static void main(String[] args){
        new UITests();
    }
}

问题是,当我在顶部面板中添加JTable时,底部面板的高度会缩小,就像其某些高度被顶部面板占据一样.这是顶部面板中表格的代码,是在设置了三个主面板的背景之后添加的.

Problem is, when I add a JTable in the top panel, the bottom panel's height is shrinked, like some of its height is taken by the top panel. Here is the code for the table in the top panel , added after setting the background of three main panels.

Object[][] contents = {{"haha","hehe", "hihi", "hoho", "huhu"},
        {"haha","hehe", "hihi", "hoho", "huhu"},
        {"haha","hehe", "hihi", "hoho", "huhu"},
        {"haha","hehe", "hihi", "hoho", "huhu"}};
Object[] heads = {"Haha Head", "Hehe Head", "Hihi Head", "Hoho Head", "Huhu Head"};
JTable table = new JTable(contents, heads);
JScrollPane scrollTable = new JScrollPane(table);

//add components to topPanel
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = gbc.BOTH;
topPanel.setLayout(new GridBagLayout());
topPanel.add(scrollTable, gbc);

框架现在看起来像

----------------------------
|   |                      |
| l |                      |
| e |  //toppanel          |
| f |  //new table created |   
| t |                      |
| p |                      |
| n |                      |
| l |----------------------|
|   |  //bottompanel       |
----------------------------

那么,如何固定'weightx'或'weighty'创建的空间?

So, how can I make the space created by 'weightx' or 'weighty' fixed?

推荐答案

weightx/weighty用于分配额外的空间.因此,实际上,容器会询问孩子自己的首选尺寸,如果尺寸小于可用尺寸,则多余的像素会根据权重进行分配.

weightx/weighty are used to distribute extra space. So in fact container asks children for their preferred size and if it's smaller than available the extra pixels are distributed according to the weights.

因此您可以定义容纳JTable

这篇关于GridBagLayout固定空间按权重x/y设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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