【问题标题】:GridBagLayout fix space set by weight x/yGridBagLayout 修复由权重 x/y 设置的空间
【发布时间】:2013-03-14 02:14:49
【问题描述】:

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

----------------------------
|   |                      |
| 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 时,底部面板的高度会缩小,就像顶部面板占用了它的一些高度一样。这是顶部面板中表格的代码,在设置三个主面板的背景后添加。

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'创建的空间呢?

【问题讨论】:

  • 问题左侧面板和底部面板可以双向调整大小
  • 它们应该被修复。

标签: java swing layout-manager gridbaglayout


【解决方案1】:

weightx/weighty 用于分配额外空间。所以实际上容器会询问孩子他们喜欢的大小,如果它比可用的小,额外的像素会根据权重分配。

因此您可以定义拥有JTable 的JScrollPane 的首选大小

【讨论】:

  • 我理解,但根据this thread setPreferredSize() 应该避免,而是让LayoutManager 处理调整大小。我想知道我是否可以在不使用该方法的情况下实现这一目标。
  • 您可以使用 GridLayout(2 行 1 列)将顶部和底部面板放置到容器中。或者您可以覆盖bottomPanel 的首选尺寸以返回topPanel 的首选高度。
  • 感谢您的建议。但我只想使用GridBagLayout 来实现这一点。听说这个布局很灵活,想知道是不是只用GridBagLayout就能实现,不干扰组件的setPrefferedSize。
  • 好吧,看来除了听从你的建议别无他法,所以回答接受。
猜你喜欢
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
  • 2013-08-01
  • 2020-06-03
  • 2023-04-04
  • 2015-05-18
  • 1970-01-01
相关资源
最近更新 更多