Last good article on gridbaglayout Networks

Source: Internet
Author: User

Gridbaglayout
[Common constructor]

Public gridbaglayout ()

[Features ]:

Gridbaglayout is the most complex among all AWT layout managers, and its functions are also the most powerful. this phenomenon stems from the numerous configurable options it provides, and you can almost completely control the layout of containers. although the complexity is obvious, it is easy to use gridbaglayout as long as you understand the basic idea.
Gridbaglayout can also be guessed from its name. Like gridlayout, gridbaglayout manages components in the form of grids in containers. However, gridbaglayout is much more powerful.
1. All rows and columns managed by gridbaglayout can be of different sizes.
2. gridlayout restricts each component to one cell, but gridlayout does not: the component can occupy any rectangular area in the container,
Gridbaglayout is usually restricted by a dedicated class, which is called gridbagconstraints. all the members are public. Therefore, to learn how to use gridbaglayout, you must first understand the constraint variables and how to set these constraint variables.
The following are public member variables of gridbagconstraints:
Public int anchor
Public int fill
Public gridheight
Public gridweight
Public girdx
Public gridy
Public insets
Public int ipadx
Public int ipady
Public double weightx
Public double weighty
It seems that many constraints need to be set, but in fact many constraints only need to be set once and reused for multiple components. Only a few items need to be modified each time a component is added.
The following is an example of gridbaglayout with simple constraints.
Public class gridbaglayoutexample2 extends jpanel {

Public gridbaglayoutexample2 (){

This. setlayout (New gridbaglayout ());
This. setopaque (true );
Gridbagconstraints c = new gridbagconstraints ();
Jbutton B = new jbutton ("one ");
C. gridx = 0;
C. gridy = 0;
C. gridwidth = 2;
C. gridheight = 1;
This. Add (B, c); // button 1 added
C. gridy ++;
B = new jbutton ("two ");
This. Add (B, c );

C. gridx = 2;
C. gridy = 0;
C. gridwidth = 2;
C. gridheight = 2;
B = new jbutton ("three ");
This. Add (B, c );

C. gridx = 0;
C. gridy = 2;
C. gridwidth = 4;
C. gridheight = 1;
This. Add (New jtextfield (35), C );
}

Public static void main (string [] ARGs ){
Jframe F = new jframe ("gridbaglayout 2 ");
Jpanel P = new gridbaglayoutexample2 ();
F. getcontentpane (). Add (P );
F. Pack ();
F. setdefaclocloseoperation (windowconstants. exit_on_close );
F. setvisible (true );

}

}

 

Chart 1

In the preceding example, the constraint Object C is generated and Its Attributes gridx, gridy, gridwidth, and girdheight are set. The following briefly introduces the functions of these four attributes.
The gridx and gridy attributes are used to describe the grid position when a component is laid out, that is, the starting position of the grid.
The gridwidth and gridheigh attributes are used to describe the number of grids occupied by the component in the layout. gridwidth describes the number of grids occupied by the component in the horizontal direction, gridheight describes the number of grids occupied by the component in the vertical direction.
You can also use gridbagconstraints's reletive and remainder to specify it. Its usage is:
Set the value of gridx to gridbagconstriants. when a component is added, it is placed on the right of the previous component. similarly, set the value of gridy to gridbagconstraints. when a component is added, it is placed below the previous component (this is a method to determine the relative placement of the current component based on the previous component)
You can also apply the remainder method of gridweight and gridheight to gridbagconstraints. The created component will be extended from the starting point of creation to the limit allowed by the container. this feature allows you to create components that span certain rows or columns and change the number of components in the corresponding direction, even if you add additional components elsewhere in the layout.
You can notice that the buttonone and buttontwo values in Figure 1 are close to each other. You can set the insets of gridbagconstraints members to adjust the space around it,
New insets (INT top, int left, int bottom, int right)
C. insets = new insets (4,4, 4,4 );

 

Chart 2

The next problem to be solved is the behavior during container scaling. the text bar is the most obvious problem, regardless of how the container is deformed, it keeps the same size. the text area should always span at the bottom of the container, but you certainly do not want the text bar to grow vertically when zooming vertically.
The weightx and weighty members are used to control how the cell itself scales during container deformation. these two attributes are both floating point values, which describe the ratio of horizontal or vertical wait for each cell to be allocated during stretching. for example, set weightx in the previous example.
Button one 0.4
Button two 0.4
Button three 0.6
Text area 1.0
In the stretching process, if 10 pixels are stretched, the button one gets 10x0.4 = 4 pixels horizontally.
The remaining six pixels of the button three are obtained. During the stretching process, the button one and the button three are enlarged in the form of 0.4: 0.6.
You can set weighty to 0 for vertical extension of the text box.

Chart 3

Chart 4

You may have noticed that although we have set weight of the text box to 1.0, it does not occupy additional available horizontal space, that is, the whole bottom row is not always occupied during horizontal stretching.
Therefore, this problem occurs because the difference between cells and components has not been clarified. The values of weightx and weighty control the degree of cell expansion when the container grows, however, they have no direct effect on components in each cell lattice. in fact, when the window is expanded, all cells of the container grow, including a horizontal row of cells in the text box. however, the text bar does not grow at all. this is because, within the allocated cell, the component growth is controlled by the fill Member of the gridbagconstraints object. It can take the following values
Gridbagconstraints. None does not increase
Gridbagconstraints. Horizontal only grows horizontally
Gridbagconstraints. Vertical only grows vertically
Gridbagconstraints. Both bidirectional Growth
When you create a gridbagconstraints object, its fill value is set to none. Therefore, when cells grow, the internal components of cells do not grow.

Chart 5

Ipadx and ipady attributes:
When gridbaglayout is used to layout containers, it considers the minimum size of each component as a constraint for space allocation. If the minimum size of a button is 30 pixels wide, the height is 20 pixels. In the associated restricted object, if ipadx is 4 and ipady is 2, the minimum size of the button will be 38 pixels horizontally and 24 pixels vertically.
Anchor attributes:
This field takes effect when the widget is smaller than the allocated cell area either horizontally or vertically. in these cases, anchor determines how the component is aligned in the available space. by default, components are fixed in the center of cells, and redundant space is evenly distributed around them. you can also specify other Alignment Methods:
Gridbagconstraints. North
Gridbagconstraints. South
Gridbagconstraints. Northwest
Gridbagconstraints. Southwest
Gridbagconstraints. Southeast
Gridbagconstraints. Northeast
Gridbagconstraints. East
Gridbagconstraints. West

Chart 6

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.