Java Layout Summary-gridbaglayout__java

Source: Internet
Author: User
GridBagLayout Layout Manager:
This is the most complex layout manager, the grid package layout. In this layout, the component size does not have to be the same.
GridBagLayout gb=new gridbaglayout ();
Containername.setlayout (GB);

The above code is to give the container a gridbaglayout. To use the grid package layout, you must also have one of its helper classes, gridbagcontraints. It contains all the information that the GridBagLayout class needs to locate and resize the component. Use the following steps:

1. Create an instance of the grid package layout and define it as the layout manager for the current container.

2). Create an instance of the Gridbagcontraints

3). Set constraints for the component.

4). Rules the layout manager about components and their constraints through methods

5. Add the component to the container.

6. Repeat these steps for each component that will be displayed.

The list of member variables for the Gridbagcontraints class is as follows:

1, gridx-components of the transverse coordinates;
2, the longitudinal coordinates of the girdy-component;

Gridx=0,gridy=0 is placed in 0 rows 0 columns, gridbagconstraints.relative is the default value, indicating that the current component is immediately after the previous component. If you do not specify the value on the line (and also do not specify GRIDX and GridY parameters), then no matter how many components are added on the same row

3, the horizontal width of the gridwidth--component, that is, the number of columns occupied by the component, which is similar to the colspan of HTML;
4, the longitudinal length of the gridheight-component, which refers to the number of lines occupied by the component, which is similar to the rowspan of HTML;

Sets the number of grids that the component spans vertically across, and their default values are 1. If the component is the last one in landscape or portrait, you can also set this value to Gridbagconstraints.remainder, and if you are the penultimate component, you can set the value to Gridbagconstraints.relative.

Gridwidth whether the width can be retained depends on whether there are enough components to occupy the position at the top/bottom of it, and if not, the position will be compressed. For example, a gridwidth=3 is set, but only one JButton is added, at which point it occupies only one grid size if there are no other components or only 1 components in the corresponding position on its upper and lower sides. If it has 3 components at/below the corresponding position, it can take up 3 grid sizes. If it has only 2 components on/below the corresponding position, it occupies 2 grid sizes.

Gridheight the height depends on whether there are enough components on the left and right sides to hold the position. This means that its maximum height is not greater than the maximum height of the left and right sides. For example, set gridheight=3, if the left and right components are only 1 lines, it only has 1 rows of height, and 2 rows occupy 2 rows of height.

5, weightx-refers to the weight of the line, tell the layout manager how to allocate additional horizontal space;
6, weighty-refers to the weight of the column, tell the layout manager how to allocate additional vertical space;

Used to set the size of the window to become larger, the proportion of each component is larger. The larger the number, the more space the component can get, and the default value is 0. For example, the weightx=0.5 of component A, the weightx=1 of component B, the space remaining when the window X axis is larger is allocated to components A and B in proportion to 1:2;

7, Anchor-tells the layout Manager component in the table space position, when the component is smaller than its display area to use this field;

There are center (default), North, northeast, east, southeast, west, northwest selection.

8, fill-can be used to control the behavior of components if the display area is larger than the area of the assembly. The control component is vertically filled, or horizontally filled, or two directions are filled together;

9, insets-refers to the component and table space around the edge of the size of the blank area, the inner margin, into the size of the component itself. It has four parameters, namely, top, left, bottom, and right, default to (0,0,0,0).

10, ipadx-components of the transverse spacing, the width of the component is the minimum width of this component and IPADX value;
11. ipady-The longitudinal spacing between components, the height of the component is the minimum height of the component plus the Ipady value.

The outer margin, the size that the final component occupies is the size of the component itself plus the outer margin.

So, if we already understand the detailed usage of gridbagconstraints, let's take a look at how it should be implemented in practical applications:
TAGLBL = new JLabel ("Tags");
C.gridx = 0; X grid position
C.gridy = 0; Y grid position
Gridbag.setconstraints (TAGLBL, C); Set the label limit

Contentpane.add (TAGLBL); Add to Content panel

What we do is sample our tag, assign it to a grid position, link it to a constraint object, and add it to our content panel.
TAGMODELBL = new JLabel ("Tag Mode");
C.gridx = 0;
C.gridy = 1;
Gridbag.setconstraints (TAGMODELBL, C);

Contentpane.add (TAGMODELBL);

Note that although we have set the value of GridX to 0 in our constraint object, we still need to reset it here-for no other reason, just to increase readability.

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.