java - GridBagLayout 修复按权重 x/y 设置的空间

标签 java swing layout-manager gridbaglayout

我有一个具有以下 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”创建的空间固定呢?

最佳答案

weightx/weighty 用于分配额外的空间。因此,事实上容器会询问 children 他们喜欢的尺寸,如果它小于可用的尺寸,则额外的像素将根据权重进行分配。

因此,您可以定义保存 JTableJScrollPane 的首选大小

关于java - GridBagLayout 修复按权重 x/y 设置的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15600565/

相关文章:

java - 如何在 2 个项目之间共享存储库和服务类

java - 如何调整 JPanel 的大小以适应 "docknorth"中的 JFrame 而不会干扰剩余的 JPanel

java - 使用 GroupLayout 后获取 JComponent 的正确大小

java - GridLayout 没有填满整个窗口

java - 使用 Linkedin Spring Boot Rest api 登录

java - 创建一个抽象基类来处理不同类型的枚举

java - 覆盖 JSP 中的默认 <%=%> 以防止 XSS Hack

java - 将组件放入较小的组件内

java - Swing JScrollPane - 如何将垂直滚动条设置为左侧?

Java 布局管理器