码迷,mamicode.com
首页 > 数据库 > 详细

java中Swing的GridBagLayout使用简介

时间:2019-12-31 23:19:28      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:需要   ane   src   参数详解   str   constrain   height   ext   extends   

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

    }
}

运行结果为:

技术图片

 

java中Swing的GridBagLayout使用简介

标签:需要   ane   src   参数详解   str   constrain   height   ext   extends   

原文地址:https://www.cnblogs.com/liyuanhong/p/12127836.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!