Swing's GridBagLayout in java Introduction

A, GridBagLayout layout manager and its layout GridBagConstraints Parameter Description

GridBagLayout primarily to the following four parameters:

columnWidths: the number of columns; example: gridBagLayout.columnWidths = new int [] { 0}; represents only a
rowHeights: The number of lines; for example: gridBagLayout.rowHeights = new int [] { 0, 0}; represents a total of 2 rows
columnWeights: setting the proportion of the width of each column; gridBagLayout.columnWeights = new double [] { 1.0}; said column width is the width of the container, i.e. the container support full
rowWeights: setting the ratio of the height of each row share; gridBagLayout.rowWeights = new double [] {0.2,0.8} ;; represents a height of the first row of containers only 2 points height, the height of the second container row representing 8 parts

GridBagContraints shell parameter settings as follows:

Before using GridBagLayout layout, you need to know the following parameters:

 

 For example the following code:

public  class ClientPanel the extends the JPanel { 

    / ** 
     * The the Create Panel. 
     * / 
    public ClientPanel () { 
        the GridBagLayout GridBagLayout = new new the GridBagLayout (); 
        gridBagLayout.columnWidths = new new  int [] {0 }; // There are provided a 
        gridBagLayout. rowHeights has = new new  int [] {0, 0 }; // there are provided two rows 
        gridBagLayout.columnWeights = new new  Double [] {1.0 }; // set the column width to the width of the container 
        gridBagLayout.rowWeights = new new Double [] {0.2,0.8 }; // height of the first row 2 parts accounted container, the height of the second row of eight containers accounted 
        setLayout (GridBagLayout); 
        
        the JPanel Panel = new new the JPanel (); 
        panel.setBackground (Color.PINK); 
        the GridBagConstraints gbc_panel = new new the GridBagConstraints (); 
        gbc_panel.insets = new new Insets (0, 0,. 5, 0 ); 
        gbc_panel.fill = GridBagConstraints.BOTH; 
        gbc_panel.gridx = 0 ; 
        gbc_panel.gridy = 0 ; 
        the Add (Panel, gbc_panel); 
        
        the JPanel Panel_1 = new 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);

    }
}

Results are as follows:

 

 The following code:

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

    }
}

运行结果为:

 

Guess you like

Origin www.cnblogs.com/liyuanhong/p/12127836.html