GridBagLayout gridheight 约束不影响结果

GridBagLayout gridheight constraint not affecting result

我正在使用 GridBag 布局开发 Java Swing 项目。我正在尝试制作两个宽度相同且水平对齐但高度不同的面板。

像这样:

我有以下代码:

import javax.swing.*;
import java.awt.*;

public class Mega extends JFrame {

    public static void main(String[] args) {

        new Mega();

    }

    public Mega() {

        Dimension minDimension = new Dimension();
        minDimension.width = 800;
        minDimension.height = 800;

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());
        this.add(mainPanel);

        GridBagConstraints constraints = new GridBagConstraints();

        JPanel topPanel = new JPanel();
        topPanel.setBackground(Color.GRAY);

        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.weightx = 1;
        constraints.weighty = 1;
        constraints.insets = new Insets(5, 5, 5, 5);
        constraints.anchor = GridBagConstraints.PAGE_START;
        constraints.fill = GridBagConstraints.BOTH;
        mainPanel.add(topPanel, constraints);

        JPanel bottomPanel = new JPanel();
        bottomPanel.setBackground(Color.GREEN);

        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.gridwidth = 1;
        constraints.gridheight = 3;
        constraints.weightx = 1;
        constraints.weighty = 1;
        constraints.insets = new Insets(5, 5, 5, 5);
        constraints.anchor = GridBagConstraints.PAGE_END;
        constraints.fill = GridBagConstraints.BOTH;
        mainPanel.add(bottomPanel, constraints);

        this.setVisible(true);

    }

}

使用此代码,底部面板的高度应为第一面板的三倍,因为它占据了三行,而顶部面板仅占据了一行。然而,我得到的是这样的。

gridheight = 3 约束似乎没有什么不同,因为两个面板的高度相同。我做错了什么?

gridHeight 不会有任何影响,因为没有更多的行可以扩展(将 gridWidthgridHeight 视为 "expand across"),相反,要么从您的组件中提供更好的尺寸提示或使用 weighty 属性

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Mega extends JFrame {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Mega();
            }
        });

    }

    public Mega() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel() {
            // This is done for demonstration purposes
            // it would be better for the child components
            // to provide appropriate sizing hints
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(800, 800);
            }
        };
        mainPanel.setLayout(new GridBagLayout());
        this.add(mainPanel);

        GridBagConstraints constraints = new GridBagConstraints();

        JPanel topPanel = new JPanel();
        topPanel.setBackground(Color.GRAY);

        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.weightx = 1;
        constraints.weighty = 0.25;
        constraints.gridwidth = 1;
        constraints.fill = GridBagConstraints.BOTH;
        constraints.insets = new Insets(5, 5, 5, 5);
        mainPanel.add(topPanel, constraints);

        JPanel bottomPanel = new JPanel();
        bottomPanel.setBackground(Color.GREEN);

        constraints.gridy = 1;
        constraints.weighty = 0.75;
        mainPanel.add(bottomPanel, constraints);

        pack();
        setLocationRelativeTo(null);
        this.setVisible(true);

    }

}