Original http://blog.csdn.net/wguoyong/article/details/6898149
It groups components by layer to determine whether they are inContainer.GroupLayoutIt is mainly used by generators, but can also be manually encoded. GroupGroupClass.GroupLayoutTwo groups are supported. Serial Group
(Sequential group) place its child elements one by one in sequence. Parallel Group can align its child elements in four ways.
Each group can contain any number of elements.Group,ComponentOr gap ). The gap can be considered as an invisible component with the minimum size, preferred size, and maximum size. In addition,GroupLayoutThe value can also be taken fromLayoutStyle.
The element is similar to a spring. Each element has a range specified by the minimum size, preferred size, and maximum size. The gap range is specified by the developer orLayoutStyleOK.ComponentRange passedComponentOfgetMinimumSize,getPreferredSizeAndgetMaximumSizeDetermine the method. In addition, addComponentYou can specify a specific range instead of the range of the component.GroupThe range is determined by the group type.ParallelGroupThe range is the maximum value of its element range.SequentialGroupThe range is the sum of its element ranges.
GroupLayoutTreat each axis separately. That is to say, there is a group that represents the horizontal axis and a group that represents the vertical axis. The horizontal group determines the minimum size, preferred size, and maximum size along the horizontal axis, and sets the X and width of the included components. The vertical group determines the minimum size, preferred size, and maximum size along the vertical axis, and sets the Y and height of the contained components. EachComponentBoth the horizontal and vertical groups must exist. Otherwise, during the layout process or during the request minimum size, preferred size, or maximum sizeIllegalStateException.
Java SE 6 contains a new grouplayout. From the word meanings of grouplayout, it manages the layout in groups, that is, multiple components (such: jlable and jbutton are divided into different groups by region, and managed based on the arrangement of each group (group) relative to the horizontal axis (horizontal) and the vertical axis (vertical. The following describes how to use this interface:
First, divide the group:
1. Divided by horizontal:
Hpg1: jlabel group (only one component can be left blank)
Hpg2a: The left two jcheckboxes are arranged in parallel with the horizontal axis (horizontal) to form a parallelgroup
Hpg2b: The two jcheckboxes on the right are arranged in parallel with the horizontal axis (horizontal) to form a parallelgroup
Hpg2h: hpg2a and hpg2b groups are arranged along the horizontal axis (horizontal) to form a sequentialgroup.
Hpg2: jtextbox and group hpg2h are arranged in parallel to the horizontal axis (horizontal) to form a parallelgroup
Hpg3: Two jtextbox are arranged in parallel to the horizontal axis (horizontal) to form a parallelgroup
In addition, jlabel, hpg2, and hpg3 are arranged along the horizontal axis (horizontal) to form a sequentialgroup.
2. Vertical:
Vpg1: jlabel, jtextbox, and jbutton (FIND) are arranged in parallel with vertical to form a parallelgroup.
Vpg2: the preceding two jcheckbox and jbutton (cancel) are arranged in parallel with the vertical axis (vertical) to form a parallelgroup.
Vpg3: the bottom two jcheckboxes are arranged in parallel to the vertical axis (vertical) to form a parallelgroup
In addition, the vpg1, vpg2, and vpg3 groups are arranged along the vertical axis (vertical) to form a sequentialgroup.
3. encoding: we must set both the horizontal and vertical directions. For the implementation method, see the program.
Addcomponent: Add components to a group, such as jlabel and jtextbox.
Addgroup: Add group to group
/// Glayout. Java
Package Lee;
Import java. AWT .*;
Import java. AWT. event .*;
Import javax. Swing .*;
Public class glayout extends jframe
{
Public glayout ()
{
Super ("find ");
Setdefaclocloseoperation (jframe. exit_on_close );
// String LAF = uimanager. getcrossplatformlookandfeelclassname ();
String LAF = uimanager. getsystemlookandfeelclassname ();
Try {
Uimanager. setlookandfeel (LAF );
} Catch (unsupportedlookandfeelexception exc ){
System. Err. println ("Warning: unsupportedlookandfeel:" + LAF );
} Catch (exception exc ){
System. Err. println ("error loading" + LAF + ":" + exc );
}
Jlabel label1 = new jlabel ("Find What :");
Jtextfield textfield1 = new jtextfield ();
Jcheckbox casecheckbox = new jcheckbox ("match case ");
Jcheckbox wholecheckbox = new jcheckbox ("whole words ");
Jcheckbox wrapcheckbox = new jcheckbox ("warp around ");
Jcheckbox backcheckbox = new jcheckbox ("Search backwards ");
Jbutton findbutton = new jbutton ("find ");
Jbutton cancelbutton = new jbutton ("cancel ");
Container c = getcontentpane ();
Grouplayout layout = new grouplayout (C );
C. setlayout (layout );
// Automatically sets the gap between components and groups.
Layout. setautocreategaps (true );
Layout. setautocreatecontainergaps (true );
// Leading -- left alignment baseline -- bottom alignment center -- center alignment
Grouplayout. parallelgroup hpg2a = layout. createparallelgroup (grouplayout. Alignment. Leading );
Hpg2a. addcomponent (casecheckbox );
Hpg2a. addcomponent (wholecheckbox );
Grouplayout. parallelgroup hpg2b = layout. createparallelgroup (grouplayout. Alignment. Leading );
Hpg2b. addcomponent (wrapcheckbox );
Hpg2b. addcomponent (backcheckbox );
Grouplayout. sequentialgroup hpg2h = layout. createsequentialgroup ();
Hpg2h. addgroup (hpg2a). addgroup (hpg2b );
Grouplayout. parallelgroup hpg2 = layout. createparallelgroup (grouplayout. Alignment. Leading );
Hpg2.addcomponent (textfield1 );
Hpg2.addgroup (hpg2h );
Grouplayout. parallelgroup hpg3 = layout. createparallelgroup (grouplayout. Alignment. Leading );
Hpg3.addcomponent (findbutton );
Hpg3.addcomponent (cancelbutton );
// Horizontal
Layout. sethorizontalgroup (layout. createsequentialgroup ()
. Addcomponent (label1). addgroup (hpg2). addgroup (hpg3 ));
// Set the width of the two buttons in the horizontal direction.
Layout. linksize (swingconstants. Horizontal, new component [] {findbutton, cancelbutton });
// Layout. linksize (swingconstants. Horizontal, new component [] {casecheckbox, wholecheckbox, wrapcheckbox, backcheckbox });
Grouplayout. parallelgroup vpg1 = layout. createparallelgroup (grouplayout. Alignment. baseline );
Vpg1.addcomponent (label1 );
Vpg1.addcomponent (textfield1 );
Vpg1.addcomponent (findbutton );
Grouplayout. parallelgroup vpg2 = layout. createparallelgroup (grouplayout. Alignment. center );
Vpg2.addcomponent (casecheckbox );
Vpg2.addcomponent (wrapcheckbox );
Vpg2.addcomponent (cancelbutton );
Grouplayout. parallelgroup vpg3 = layout. createparallelgroup (grouplayout. Alignment. baseline );
Vpg3.addcomponent (wholecheckbox );
Vpg3.addcomponent (backcheckbox );
// Vertical
Layout. setverticalgroup (layout. createsequentialgroup ()
. Addgroup (vpg1). addgroup (vpg2). addgroup (vpg3 ));
Setlocation (200,200 );
Pack ();
Setvisible (true );
}
Public static void main (string [] ARGs)
{
New glayout ();
}
}