GridBagLayout is one of the most important layout managers in Java, can make a very complex layout, can say that gridbaglayout is necessary to learn,
GridBagLayout
Class is a flexible layout manager that does not require components to be of the same size to align components vertically, horizontally, or along their baselines.
Each GridBagLayout
object maintains a dynamic rectangular cell grid, each of which occupies one or more of these units, which is called the display area .
Here is a Notepad case to illustrate how gridbaglayout is used.
Analysis:
A description with arrows can be stretched.
4 occupies 4 compartments and 6 occupies 4 compartments. If the setting 6 can be stretched, then 4 will also be stretched.
However, if you set the 4 stretch, the 7 column can also be stretched, so 4 cannot set the extrusion. We should set 4 to be followed by 6 for stretching.
The Gray line is to see the approximate layout, the number of squares occupied by the component.
Run-time display effect
ImportJavax.swing.JPanel; Public classGridbagdemoextendsJFrame { Public Static voidMain (String args[]) {Gridbagdemo demo=NewGridbagdemo (); } PublicGridbagdemo () {init (); This. SetSize (600,600); This. setvisible (true); } Public voidinit () {J1=NewJButton ("Open"); J2=NewJButton ("Save"); J3=NewJButton ("Save As"); J4=NewJPanel (); string[] Str= {"Java notes", "C # Notes", "HTML5 notes" }; J5=NewJComboBox (str); J6=NewJTextField (); J7=NewJButton ("Empty"); J8=NewJList (str); J9=NewJTextArea (); J9.setbackground (color.pink);//to see the effect, set the colorGridBagLayout layout =NewGridBagLayout (); This. setlayout (layout); This. Add (J1);//Add components into JFrame This. Add (J2); This. Add (J3); This. Add (J4); This. Add (J5); This. Add (J6); This. Add (J7); This. Add (J8); This. Add (J9); gridbagconstraints s=NewGridbagconstraints ();//define a gridbagconstraints,//is used to control the display location of the added componentS.fill =Gridbagconstraints.both; //This method is intended to set the display if the area of the component is larger than the component itself//None: The component size is not resized. //Horizontal: Widens the component so that it fills its display area horizontally, but does not change the height. //VERTICAL: The heightening component, which fills its display area vertically, but does not change the width. //BOTH: Causes the component to fully fill its display area. s.gridwidth=1;//the method is to set the number of squares occupied by the component level, and if 0, it means that the component is the last of the rowS.WEIGHTX = 0;//This method sets the horizontal stretch of the component, if 0 means no stretch, and does not 0 as the window grows, between 0 and 1s.weighty=0;//This method sets the vertical stretch of the component, if 0 means no stretch, not 0 stretches as the window grows, between 0 and 1Layout.setconstraints (J1, s);//Set up ComponentsS.gridwidth=1; S.WEIGHTX= 0; S.weighty=0; Layout.setconstraints (J2, s); S.gridwidth=1; S.WEIGHTX= 0; S.weighty=0; Layout.setconstraints (J3, s); S.gridwidth= 0;//the method is to set the number of squares occupied by the component level, and if 0, it means that the component is the last of the rowS.WEIGHTX = 0;//cannot be a 4-1,j4 for a, and can be stretched horizontally,//However, if 1, the columns of the subsequent rows will also be stretched, causing the column where the J7 is to be stretched//so it should be stretched with J6 .S.weighty=0; Layout.setconstraints (J4, s); S.gridwidth=2; S.WEIGHTX= 0; S.weighty=0; Layout.setconstraints (J5, s); ; S.gridwidth=4; S.WEIGHTX= 1; S.weighty=0; Layout.setconstraints (J6, s); ; S.gridwidth=0; S.WEIGHTX= 0; S.weighty=0; Layout.setconstraints (J7, s); ; S.gridwidth=2; S.WEIGHTX= 0; S.weighty=1; Layout.setconstraints (J8, s); ; S.gridwidth=5; S.WEIGHTX= 0; S.weighty=1; Layout.setconstraints (J9, s); } JButton J1; JButton J2; JButton J3; JPanel J4; JComboBox J5; JTextField J6; JButton J7; JList J8; JTextArea J9; }