Layout Manager
Containers can hold various components, and the location and size of the components are determined by the layout manager within the container. The following 5 layout managers are available for us in AWT:
①flowlayout Streaming layout Manager
②borderlayout Boundary layout Manager
③gridlayout Grid Layout Manager
④cradlayout Card Layout Manager
⑤gridbaglayout Grid Package Layout Manager
The layout of components in a container is usually controlled by the layout manager. Each container (such as a panel or a frame) has a default layout manager associated with it, and the Panel container defaults to the Flowlayout,frame container default is BorderLayout, which we can call setlayout () to change the layout manager;
You can control the size and position of the component by setting the empty layout manager. You can call setlayout (null).
After you set up the empty layout manager, you must call setlocation (), setSize (), or setbounds () on all components to locate them in the container.
Streaming layout Manager
1 classMyFrame3extendsFrame {2 PublicMyFrame3 (String title) {3 Super(title);4 }5 6 Public voidinit () {7FlowLayout layout=NewFlowLayout (Flowlayout.left);//set the left alignment8 This. setlayout (layout);9 This. SetBackground (Color.cyan);Ten This. Add (NewButton ("Btn1")); One This. Add (NewButton ("Btn2")); A This. Add (NewButton ("Btn3")); - This. Add (NewButton ("Btn4")); - This. Add (NewButton ("Btn5")); the This. Add (NewButton ("Btn6")); - This. Add (NewButton ("Btn7")); - This. Add (NewButton ("Btn8")); - This. setSize (300, 300); + This. setvisible (true); - } +}
Boundary Layout Manager
1 classMyFrame4extendsFrame {2 PublicMyFrame4 (String title) {3 Super(title);4 }5 6 Public voidinit () {7 This. SetBackground (Color.cyan);8 This. Add (NewButton ("Btn1"), borderlayout.east);9 This. Add (NewButton ("Btn2"), borderlayout.west);Ten This. Add (NewButton ("Btn3"), Borderlayout.north); One This. Add (NewButton ("Btn4"), Borderlayout.south); A This. Add (NewButton ("Btn5"), borderlayout.center); - This. setSize (300, 300); - This. setvisible (true); the } -}
Grid Layout Manager
1 classMyFrame5extendsFrame {2 PublicMyFrame5 (String title) {3 Super(title);4 }5 6 Public voidinit () {7GridLayout layout=NewGridLayout (3,2);//Create a grid of 3 rows and 2 columns8 This. setlayout (layout);9 This. SetBackground (Color.cyan);Ten This. Add (NewButton ("Btn1")); One This. Add (NewButton ("Btn2")); A This. Add (NewButton ("Btn3")); - This. Add (NewButton ("Btn4")); - This. Add (NewButton ("Btn5")); the This. setSize (300, 300); - This. setvisible (true); - } -}
Card Layout Manager
1 classMyFrame6extendsFrame {2 PrivatePanel cardpanel=NULL;3 PrivatePanel ctrolpanel=NULL;4 PrivateCardLayout cardlayout=NULL;5 PrivateFlowLayout flowlayout=NULL;6 PrivateLabel lb1,lb2,lb3,lb4;7 PrivateButton btnfirst,btnprevious,btnnext,btnlast;8 PrivateTextField txtcontent;9 PublicMyFrame6 (String title) {Ten Super(title); One } A - Public voidinit () { - //Create 2 panel containers theCardpanel=NewPanel (); -Ctrolpanel=NewPanel (); - - //Create 2 layout managers +cardlayout=Newcardlayout (); -flowlayout=NewFlowLayout (); + A //to set the specified layout manager for a container atCardpanel.setlayout (CardLayout);//placing card layouts in card containers -Ctrolpanel.setlayout (FlowLayout);//control Container Placement Flow layout - - //declares the creation of 4 label controls and a text box control -lb1=NewLabel ("first page content", label.center); -Lb2=NewLabel ("second page content", label.center); inTxtcontent=NewTextField ();//Edit text Box -lb3=NewLabel ("fourth page content", label.center); tolb4=NewLabel ("fifth page content", label.center); + - //building Four button objects thebtnfirst=NewButton ("First"); *btnprevious=NewButton ("Previous"); $btnnext=NewButton ("Next");Panax Notoginsengbtnlast=NewButton ("Last"); - Ctrolpanel.add (btnfirst); the Ctrolpanel.add (btnprevious); + Ctrolpanel.add (btnnext); A Ctrolpanel.add (btnlast); the + //add four Label controls and a text box control to the card container - Cardpanel.add (LB1); $ Cardpanel.add (LB2); $ Cardpanel.add (txtcontent); - Cardpanel.add (LB3); - Cardpanel.add (LB4); the This. Add (Cardpanel,borderlayout.center);//Place the card container in the middle - This. Add (Ctrolpanel,borderlayout.south);//Place the control container in the SouthWuyi This. setSize (400, 300); the This. setvisible (true); - } Wu}
AWT Layout Manager