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布局方式之前,需要了解下面的参数:

java中Swing的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);

    }
}

运行结果如下:

java中Swing的GridBagLayout使用简介

 

 以下代码:

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

    }
}

运行结果为:

java中Swing的GridBagLayout使用简介

 

上一篇:weui 的tabbar如何固定在底部


下一篇:宝塔linux面板命令大全