GridBagLayout网格高度约束不影响结果

时间:2018-06-21 21:47:03

标签: java swing layout jpanel layout-manager

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

赞: enter image description here

我有以下代码:

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);

    }

}

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

enter image description here

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

1 个答案:

答案 0 :(得分:2)

gridHeight不会有任何影响,因为没有更多行可扩展(将gridWidthgridHeight视为“扩展”),而是提供更好的大小来自组件的提示或使用weighty属性

Example

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);

    }

}