Learning Links: http://www.cnblogs.com/taoweiji/archive/2012/12/10/2812221.html
GroupLayoutis one LayoutManager that groups components hierarchically to determine their Container position in. It GroupLayout is primarily intended for use by generators, but can also be coded manually. The grouping Group is done by an instance of the class. GroupLayouttwo groups are supported. Serial groups (sequential group) place their child elements sequentially, one after the other. The parallel group (parallel group) is able to align its child elements in four ways.
Each group can contain any number of elements, where the element has Group , Component or gaps (GAP). A gap can be treated as an invisible component with a minimum size, a preferred size, and a maximum size. In addition, the GroupLayout preferred clearance whose value is taken is also supported LayoutStyle .
GroupLayout必须要设置它的GroupLayout.setHorizontalGroup和GroupLayout.setVerticalGroup。
Grouplayout.sethorizontalgroup refers to the level of determination, the following example "account" and "password" is a level, the other components are also a level. Please see the code for details
Grouplayout.setverticalgroup. is determined by the vertical. Their level is to set the priority level of the component according to the group, and the higher the level, the higher it appears.
Grouplayout.sethorizontalgroup (Sequentialgroup (Parallelgroup (component)));
Example:
Packagetest;Importjavax.swing.GroupLayout;Importjavax.swing.GroupLayout.Alignment;ImportJavax.swing.JButton;ImportJavax.swing.JLabel;ImportJavax.swing.JPasswordField;ImportJavax.swing.JRadioButton;ImportJavax.swing.JTextField; Public classTestextendsJavax.swing.JFrame { Public Static voidMain (string[] args) {test F=Newtest (); } JLabel Label1; JLabel Label2; JLabel Label3; JTextField TF; JPasswordField psf; Jradiobutton Rb1; Jradiobutton Rb2; JButton bt1; JButton BT2; PublicTest () { This. setvisible (true); This. SetSize (250, 220); This. setvisible (true); This. setlocation (400, 200); Label1=NewJLabel ("China soft BBS Fast Landing"); Label2=NewJLabel ("Account number:"); Label3=NewJLabel ("Password:"); TF=NewJTextField (); PSF=NewJPasswordField (); Rb1=NewJradiobutton ("Remember Password"); Rb2=NewJradiobutton ("Automatic login"); BT1=NewJButton ("Login"); //creates a grouplayout for the specified ContainerGrouplayout layout =NewGrouplayout ( This. Getcontentpane ()); This. Getcontentpane (). setlayout (layout); //Create a horizontal continuous group of grouplayout, with the first Parallelgroup added, the higher the priority level. Grouplayout.sequentialgroup Hgroup =Layout.createsequentialgroup (); Hgroup.addgap (5);//Add IntervalHgroup.addgroup (Layout.createparallelgroup (). AddComponent (Label2). AddComponent (LABEL3) ); Hgroup.addgap (5); Hgroup.addgroup (Layout.createparallelgroup (). AddComponent (Label1). AddComponent (PSF). AddComponent (RB1). Add Component (RB2). addcomponent (TF). AddComponent (BT1)); Hgroup.addgap (5); Layout.sethorizontalgroup (Hgroup); //Create a vertical continuous group of grouplayout, with the first Parallelgroup added, the higher the priority level. Grouplayout.sequentialgroup Vgroup =Layout.createsequentialgroup (); Vgroup.addgap (10); Vgroup.addgroup (Layout.createparallelgroup (). AddComponent (Label1)); Vgroup.addgap (10); Vgroup.addgroup (Layout.createparallelgroup (). AddComponent (Label2). AddComponent (TF)); Vgroup.addgap (5); Vgroup.addgroup (Layout.createparallelgroup (). AddComponent (LABEL3). AddComponent (PSF)); Vgroup.addgroup (Layout.createparallelgroup (). AddComponent (RB1)); Vgroup.addgroup (Layout.createparallelgroup (). AddComponent (RB2)); Vgroup.addgroup (Layout.createparallelgroup (alignment.trailing). AddComponent (BT1)); Vgroup.addgap (10); //set up vertical groupsLayout.setverticalgroup (Vgroup); } }
:
Learn Java layout grouplayout