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

    }
}

運行結果為:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM