• java中Swing的GridBagLayout使用简介


    一、GridBagLayout 布局管理器以及其GridBagConstraints布局参数详解

    GridBagLayout主要使用到以下4个参数:

    columnWidths:设置列数;例如:gridBagLayout.columnWidths = new int[]{0};   表示只有一列
    rowHeights:设置行数;例如:gridBagLayout.rowHeights = new int[]{0, 0}; 表示总共有2行
    columnWeights:设置各列所占宽度比例;gridBagLayout.columnWeights = new double[]{1.0};  表示,列的宽度为容器的宽度,即撑满容器
    rowWeights:设置各行所占的高度比例;gridBagLayout.rowWeights = new double[]{0.2,0.8};;表示第一行的高度只占容器高度的2分,第二行的高度占容器的8份

    GridBagContraints壳设置的参数如下:

    在使用GridBagLayout布局方式之前,需要了解下面的参数:

     例如如下代码:

    public class ClientPanel extends JPanel {
    
        /**
         * Create the panel.
         */
        public ClientPanel() {
            GridBagLayout gridBagLayout = new GridBagLayout();
            gridBagLayout.columnWidths = new int[]{0};  //设置了总共有一列
            gridBagLayout.rowHeights = new int[]{0, 0};  //设置了总共有2行
            gridBagLayout.columnWeights = new double[]{1.0};  //设置了列的宽度为容器宽度
            gridBagLayout.rowWeights = new double[]{0.2,0.8};  //第一行的高度占了容器的2份,第二行的高度占了容器的8份
            setLayout(gridBagLayout);
            
            JPanel panel = new JPanel();
            panel.setBackground(Color.PINK);
            GridBagConstraints gbc_panel = new GridBagConstraints();
            gbc_panel.insets = new Insets(0, 0, 5, 0);
            gbc_panel.fill = GridBagConstraints.BOTH;
            gbc_panel.gridx = 0;
            gbc_panel.gridy = 0;
            add(panel, gbc_panel);
            
            JPanel panel_1 = new JPanel();
            panel_1.setBackground(Color.ORANGE);
            GridBagConstraints gbc_panel_1 = new GridBagConstraints();
            gbc_panel_1.fill = GridBagConstraints.BOTH;
            gbc_panel_1.gridx = 0;
            gbc_panel_1.gridy = 1;
            add(panel_1, gbc_panel_1);
    
        }
    }

    运行结果如下:

     以下代码:

    public class ClientPanel extends JPanel {
    
        /**
         * Create the panel.
         */
        public ClientPanel() {
            GridBagLayout gridBagLayout = new GridBagLayout();
            gridBagLayout.columnWidths = new int[]{0, 0, 0,0};  //设置了4列
            gridBagLayout.rowHeights = new int[]{0, 0};   //设置了2行
            gridBagLayout.columnWeights = new double[]{0.25,0.25,0.25,0.25};
            gridBagLayout.rowWeights = new double[]{0.2,0.8};
            setLayout(gridBagLayout);
            
            JPanel panel = new JPanel();
            panel.setBackground(Color.PINK);
            GridBagConstraints gbc_panel = new GridBagConstraints();
            gbc_panel.insets = new Insets(0, 0, 5, 0);
            gbc_panel.fill = GridBagConstraints.BOTH;
            gbc_panel.gridx = 3;
            gbc_panel.gridy = 0;
            add(panel, gbc_panel);
            
            JPanel panel_1 = new JPanel();
            panel_1.setBackground(Color.ORANGE);
            GridBagConstraints gbc_panel_1 = new GridBagConstraints();
            gbc_panel_1.insets = new Insets(0, 0, 0, 5);
            gbc_panel_1.fill = GridBagConstraints.BOTH;
            gbc_panel_1.gridx = 0;
            gbc_panel_1.gridy = 1;
            add(panel_1, gbc_panel_1);
    
        }
    }

    运行结果为:

  • 相关阅读:
    hmset
    java 调用mongo 判断大于等于 并且小约等于<=
    Maven项目,别人的没问题,自己机器一直有问题
    linux 时间datetimectl 问题
    真正手把手教 git
    0324-SQLMAP使用参数备注
    安全推荐网址:
    JavaScript Base64 作为文件上传的实例代码解析
    学习笔记|变量的解构赋值
    学习笔记|let 和 const 命令
  • 原文地址:https://www.cnblogs.com/liyuanhong/p/12127836.html
Copyright © 2020-2023  润新知