Gridbaglayout and gridbagconstraints usage

Source: Internet
Author: User

Gridbaglayout is a flexible layout manager. If you want to add components to it, you need to use gridbagconstraints. There are several parameters, which are explained as follows:

Gridx/gridy: horizontal and vertical coordinates of the component

Gridwidth: the number of columns occupied by the component. It is also the width of the component.

Gridheight: the number of lines occupied by the component. It is also the height of the component.

Fill: When the component cannot fill its space in its grid, the fill value is used to set the filling mode. There are four values.

Ipadx: Horizontal spacing between components

Ipady: Vertical spacing between components

Insets: When the component cannot be filled with its cells, The insets are used to specify the four surrounding gaps (that is, the gap between the upper and lower sides ).

Anchor: When the component cannot fill its grid, you can use anchor to set the position of the component. Anchor has two values: absolute and relative values. You can view them in this document.

Weightx: the weight of a row. This attribute determines how to allocate the remaining space of the row.

Weighty: weight of a column. This attribute determines how to allocate the remaining space of the column.

 

 

1. Make it clear that each gridbaglayout object maintains a dynamic rectangular cell grid. Each component occupies one or more of these cells, which are called display areas.
The overall direction of the Grid depends on the componentorientation attribute of the container. For the horizontal orientation from left to right, the grid coordinate (0, 0) is located in the upper left corner of the container, where x increments to the right, and y increments downward.

2. To use gidbaglayout, you must first define a gridbagconstraints object.
Java API Description: "Each component managed by gridbaglayout is associated with a gridbagconstraints instance. The constraints object specifies the display area of the component in the grid and the placement mode of the component in the display area ."
For example, you can add other components using the following code:
Gridbaglayout gridbag = new gridbaglayout ();
Gridbagconstraints c = new gridbagconstraints ();
Jframe F = new jframe ();
F. setlayout (gridbag );
Button button = new button (name );
Gridbag. setconstraints (button, C );
F. Add (jbutton );

3. to effectively use the grid package layout, you must customize one or more gridbagconstraints objects associated with the component.
You must set the properties of the gridbagconstraints object. I think gidbaglayout can be used as long as you can master the following four parameters:
(1) gridbagconstraints. gridwidthgridbagconstraints. gridheight
Specify the number of units in the display area row (for gridwidth) or column (for gridheight) of the component. The default value is 1. The following is an example of adding a button that occupies two cells (two rows and one column) to the window:
Jframe F = new jframe ();
Gridbaglayout gridbag = new gridbaglayout ();
Gridbagconstraints c = new gridbagconstraints ();
F. setlayout (gridbag );
C. gridheight = 2;
C. gridwidth = 1;
Jbutton = new jbutton ("button 1 ");
Gridbag. setconstraints (button, C );
F. Add (jbutton );
(2) gridbagconstraints. Fill
When the display area of the component is greater than the size required by the component, it is used to determine whether (and how) to adjust the component.
Possible values: gridbagconstraints. None (default ),
Gridbagconstraints. Horizontal (widening the component until it is sufficient to fill its display area horizontally without changing its height ),

Gridbagconstraints. Vertical (add the component until it is sufficient to fill its display area vertically without changing its width) and

Gridbagconstraints. Both (to make the component fully fill its display area ).
Scenario example: Add a button (the original size is 40*30) in a large window (for example, 300*300 ).

(3) gridbagconstraints. Anchor
When a component is smaller than its display area, it is used to determine where the component is placed (in the display area ). There are two possible values: relative and absolute. The relative value is relative to the componentorientation attribute of the container, but the absolute value is not. In my opinion, only the absolute value can be used. Valid values:
Absolute Value
Gridbagconstraints. North
Gridbagconstraints. South
Gridbagconstraints. West
Gridbagconstraints. East
Gridbagconstraints. Northwest
Gridbagconstraints. Northeast
Gridbagconstraints. Southwest
Gridbagconstraints. Southeast
Gridbagconstraints. Center (the default)

(4) gridbagconstraints. weightx, gridbagconstraints. weighty (the most important attribute)
This method is used to determine the distribution space, which is important for specifying adjustment behavior. For example, add two buttons (or panels) in a large window (such as 300*300) (the original size is 40*30). The default value is, you will find that the two buttons are in the upper and lower equal size areas, and only occupy a small part. The areas not occupied by the buttons are called additional areas. This additional area will be allocated with the weightx and weighty parameters.

The complete sample code is as follows:

Import javax. Swing .*;
Import java. util .*;
Import java. AWT .*;

Public class example {

Public example (){
}

Public static void main (string ARGs []) {
Jframe F = new jframe ("gridbag layout example ");

Gridbaglayout gridbag = new gridbaglayout ();
Gridbagconstraints c = new gridbagconstraints ();
F. setlayout (gridbag );
// Add button 1
C. Fill = gridbagconstraints. Both;
C. gridheight = 2;
C. gridwidth = 1;
C. weightx = 0.0; // The default value is 0.0.
C. weighty = 0.0; // The default value is 0.0.
C. Anchor = gridbagconstraints. Southwest;
Jbutton jbutton1 = new jbutton ("button 1 ");
Gridbag. setconstraints (jbutton1, C );
F. Add (jbutton1 );
// Add button 2
C. Fill = gridbagconstraints. None;
C. gridwidth = gridbagconstraints. remainder;
C. gridheight = 1;
C. weightx = 1.0; // The default value is 0.0.
C. weighty = 0.8;
Jbutton jbutton2 = new jbutton ("button 2 ");
Gridbag. setconstraints (jbutton2, C );
F. Add (jbutton2 );
// Add button 3
C. Fill = gridbagconstraints. Both;
C. gridwidth = 1;
C. gridheight = 1;
C. weighty = 0.2;
Jbutton jbutton3 = new jbutton ("button 3 ");
Gridbag. setconstraints (jbutton3, C );
F. Add (jbutton3 );

F. setdefaclocloseoperation (jframe. exit_on_close );
F. setsize (500,500 );
F. setvisible (true );
}
}

 

Gridx, gridy -- set the component location,
When gridx is set to gridbagconstraints. Relative, this component is located on the right of the previously added component.
When gridy is set to gridbagconstraints. Relative, this component is located under the previously added component.
We recommend that you define the location of gridx and gridy for later maintenance. When gridx = 0 and gridy = 0, it is placed in 0 rows and 0 columns.

Gridwidth, gridheight -- used to set the length and height of the component. The default value is 1.
You can use the gridbagconstraints. remainder constant to indicate that this component occupies all the remaining space for this row or the last component of this column.

Weightx and weighty -- used to set the ratio of each component to increase as the window grows.
A larger number indicates that the component can obtain more space. The default value is 0.

Anchor-where to place a widget when the widget space is larger than the widget space.
The options include Center (default), North, Northeast, east, southeast, west, and northwest.

Insets -- sets the spacing between components.
It has four parameters: Top, left, bottom, and right. The default value is (0, 0, 0 ).

Ipadx and ipady -- set the component spacing. The default value is 0.

All the settings in gridbaglayout must be passed through gridbagconstraints. Therefore, when we set the parameters of gridbagconstraints
After that, a new gridbagconstraints object must be created for gridbaglayout.

Constructor:
Girdbaglayout () creates a new gridbaglayout manager.
Gridbagconstraints () creates a new gridbagconstraints object.
Gridbagconstraints (INT gridx, int gridy,
Int gridwidth, int gridheight,
Double weightx, double weighty,
Int anchor, int fill, insets,
Int ipadx, int ipady) to create a new gridbagconstraints object and specify its parameter value.

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.