GridBagLayout of java Layout

Source: Internet
Author: User

GridBagLayout is similar to GridLayout, but it is more flexible and complex, because the width and height values of each unit can be different. GridBagLayout uses the constraints of GridBagLayoutConstraints to add constraints to components. There are several important parameters for GridBagLayoutConstraints. We only need to understand this, and the rest will be easy.

1: gridx, gridy:

This indicates the horizontal index and vertical index of the component, int type. The index points to the corresponding cell.

2: gridwidth, gridheight

This indicates the number of cells occupied by a component, int type. First, the large number of components does not mean that the components are large, because the surrounding area may be blank. Note that the value of gridwidth and gridheight can exceed the maximum number of components in a row and the maximum number of components in a column, after the value is exceeded, the system converts it to the maximum value of a row or column. For example, if GridBagLayout is an m×n grid, the maximum valid value of Gridwidth is M, and the maximum valid value of gridheight is N.

The default value is 1.

3: weightx, weighty

Specify how to distribute extra horizontal and vertical spaces. This means that the larger the weight, the larger the blank space around the component when the window area changes. It is like expansion. The original component 1 occupies a larger unit space than component 2, after expansion, it occupies more space than component 2.

The default value is 0.

4: fill

This specifies how to fill the blank space occupied by the component, including HORIZENTAL, VERTICAL, BOTH, and NULL.

The default value is NULL.

5: anchor

This mainly specifies how the component is displayed in the display area. To put it bluntly, alignment is a problem.

6: insets

This is an Inset class that specifies the distance between the component and its upper, left, bottom, and right, in pixels, not cells. The default value is (0, 0, 0)

Insets (int top, int left, int bottom, int right)

7: ipadx, ipady

Specify the internal width and height of the component. The size of the component is equal to the minimum width of the component. The height is added with the corresponding ipadx and ipady.

The default value is 0.

/* * GirdBagLayoutDemo.java requires no other files. */ import java.awt.Container;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Insets;import javax.swing.JButton;import javax.swing.JFrame; public class GridBagLayouts extends JFrame{private JButton btn1 = new JButton("Button1");private JButton btn2 = new JButton("Button2");private JButton btn3 = new JButton("Button3 what a fine day");private JButton btn4 = new JButton("Button4 what a fine da");private JButton btn5 = new JButton("Button5 what a fine d");private JButton btn6 = new JButton("Button6 what a fines");GridBagLayouts(){super();initComponent();}private void initComponent(){Container container = this.getContentPane();container.setLayout(new GridBagLayout());GridBagConstraints ctr1 = new GridBagConstraints();ctr1.gridx = 0;ctr1.gridy = 0;ctr1.weighty = 3;//ctr1.weightx = 1;ctr1.gridwidth = 4;ctr1.gridheight = 1;//ctr1.anchor = GridBagConstraints.WEST;ctr1.fill = GridBagConstraints.HORIZONTAL;container.add(btn1,ctr1);GridBagConstraints ctr2 = new GridBagConstraints();ctr2.gridx = 0;ctr2.gridy = 1;ctr2.weighty = 2;//ctr2.weightx = 2;ctr2.gridwidth = 2;ctr2.gridheight = 1;//ctr2.anchor = GridBagConstraints.WEST;ctr2.fill = GridBagConstraints.HORIZONTAL;container.add(btn2,ctr2);GridBagConstraints ctr3 = new GridBagConstraints();ctr3.gridx= 2;ctr3.gridy = 1;ctr3.weighty = 2;//ctr3.weightx = 1;ctr3.gridwidth = 1;ctr3.gridheight = 1;//ctr3.anchor = GridBagConstraints.WEST;ctr3.fill = GridBagConstraints.HORIZONTAL;ctr3.insets = new Insets(0,20,0,0);//top,left,bottom,rightctr3.ipady = 40;container.add(btn3,ctr3);GridBagConstraints ctr4 = new GridBagConstraints();ctr4.gridx = 0;ctr4.gridy = 2;ctr4.weighty = 1;//ctr4.weightx = 3;ctr4.gridwidth = 1;ctr4.gridheight = 1;//ctr4.anchor = GridBagConstraints.WEST;ctr4.fill = GridBagConstraints.HORIZONTAL;container.add(btn4,ctr4);GridBagConstraints ctr5 = new GridBagConstraints();ctr5.gridx = 1;ctr5.gridy = 2;ctr5.weighty = 1;//ctr5.weightx = 2;ctr5.gridwidth = 1;ctr5.gridheight = 1;//ctr5.anchor = GridBagConstraints.WEST;ctr5.fill = GridBagConstraints.HORIZONTAL;container.add(btn5,ctr5);GridBagConstraints ctr6 = new GridBagConstraints();ctr6.gridx = 2;ctr6.gridy = 2;ctr6.weighty = 1;//ctr6.weightx = 1;ctr6.gridwidth = 2;ctr6.gridheight = 1;ctr6.fill = GridBagConstraints.HORIZONTAL;container.add(btn6,ctr6);}public static void main(String[] args){GridBagLayouts frame = new GridBagLayouts();frame.pack();frame.setVisible(true);}}

Program running result:


We can see that

1: for such a grid Layout, we can ignore some grid component settings. Not every grid must have components.

2: For weights, vertical comparison and horizontal comparison are available. During the comparison, the size of the component weights varies according to the column (ROW), and the spacing is increased according to the weights in the window expansion. For example, if the vertical weight of button1 is the largest, the blank space around the window will become larger.

3: inset is measured in pixels, indicating the spacing between components.

4: ipadx, ipady indicates the internal distance of the component.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.