【问题标题】:GridBagLayout gridheight constraint not affecting resultGridBagLayout 网格高度约束不影响结果
【发布时间】:2018-12-01 08:04:08
【问题描述】:

我正在使用 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 约束看起来没有什么不同,因为两个面板的高度相同。我做错了什么?

【问题讨论】:

    标签: java swing layout jpanel layout-manager


    【解决方案1】:

    gridHeight 不会有任何影响,因为没有更多行可以扩展(将gridWidthgridHeight 视为“扩展”),相反,要么从您的组件中提供更好的尺寸提示或播放使用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);
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 2012-01-06
      • 2020-01-10
      相关资源
      最近更新 更多