ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java中Swing的GridBagLayout使用简介

2019-12-31 23:01:21  阅读:498  来源: 互联网

标签:java gbc GridBagConstraints GridBagLayout gridBagLayout JPanel Swing new panel


一、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,gbc,GridBagConstraints,GridBagLayout,gridBagLayout,JPanel,Swing,new,panel
来源: https://www.cnblogs.com/liyuanhong/p/12127836.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有