Simple understanding of layout management

Source: Internet
Author: User

 

1. borderlayout

Divide the interface into five parts: east, south, west, north, and middle.

If you want to add more widgets, you can create multiple panels and then create widgets in the panel.

By default, components are placed in the center direction.

Import java. awt. button; <br/> import java. awt. frame; <br/> public class TestBoderLayout extends Frame {<br/> public TestBoderLayout () {<br/> add (new Button ("East"), "East "); <br/> add (new Button ("West"), "West"); <br/> add (new Button ("Center"), "Center "); <br/> add (new Button ("North"), "North"); <br/> add (new Button ("South"), "South "); <br/>}< br/> public static void main (String [] args) {<br/> TestBoderLayout mainFrame = new TestBoderLayout (); <br/> mainFrame. setTitle ("BorderLayout"); <br/> mainFrame. setSize (300,200); <br/> mainFrame. setVisible (true); <br/>}< br/>}

2. flowlayout

It is the default layout manager in the panel.

If one row cannot be placed, It will be placed in the next row.

Public class testboderlayout extends frame {<br/> Public testboderlayout () {<br/> setlayout (New flowlayout ()); <br/> Add (New button ("East"); <br/> Add (New button ("West ")); <br/> Add (New button ("center"); <br/> Add (New button ("North ")); <br/> Add (New button ("South"); <br/>}< br/> Public static void main (string [] ARGs) {<br/> testboderlayout mainframe = new testboderlayout (); <br/> mainframe. settitle ("flowlayout"); <br/> mainframe. setsize (300,200); <br/> mainframe. setvisible (true); <br/>}< br/>} 

 

3. gridlayout

Containers are divided into several rows and columns of the Grid. When adding components to the container, they are arranged in the grid in the order from left to right and from top to bottom, number of rows and columns in the grid to be specified on the container

4. cardlayout

Multiple components can be displayed alternately in the same container area, which is equivalent to multiple cards stacked together and only visible at the top.

Import Java. AWT. *; <br/> Import Java. AWT. event. *; <br/> public class testcardlayout extends frame {<br/> panel plwest = new Panel (); <br/> panel plcenter = new Panel (); <br/> cardlayout Cl = new cardlayout (); <br/> Public testcardlayout () {<br/> plwest. setlayout (New gridlayout (3, 1); <br/> button btnprev = new button ("Prev"); <br/> button btnnext = new button ("Next "); <br/> button btnthree = new button ("three"); <br/> plwest. add (btnprev); <br/> plwest. add (btnnext); <br/> plwest. add (btnthree); <br/> plcenter. setlayout (CL); <br/> plcenter. add (New button ("one"), "1"); <br/> plcenter. add (New button ("two"), "2"); <br/> plcenter. add (New button ("three"), "3"); <br/> plcenter. add (New button ("four"), "4"); <br/> plcenter. add (New button ("five"), "5"); <br/> class myactionlistener implements actionlistener {<br/> Public void actionreceivmed (actionevent E) {<br/> If (E. getactioncommand (). equals ("Prev") {<br/> Cl. previous (plcenter); <br/>}< br/> else if (E. getactioncommand (). equals ("Next") {<br/> Cl. next (plcenter); <br/>}< br/> else {<br/> Cl. show (plcenter, "3"); <br/>}< br/> Add (plwest, "West "); <br/> Add (plcenter); <br/> myactionlistener MA = new myactionlistener (); <br/> btnprev. addactionlistener (MA); <br/> btnnext. addactionlistener (MA); <br/> btnthree. addactionlistener (MA); <br/>}< br/> Public static void main (string [] ARGs) {<br/> testcardlayout mainframe = new testcardlayout (); <br/> mainframe. settitle ("cardlayout"); <br/> mainframe. setsize (300,200); <br/> mainframe. setvisible (true); <br/>}< br/>} 

5. gridbaglayout

King of layout manager...

6. Cancel the layout manager on the container

Container. setlayout (null) cancels layout manager settings

Component. setbounds method to set the size and position of each component

Cancel the layout manager and change the window size. The component size does not adapt to the window change.

Import Java. AWT. *; <br/> Import Java. AWT. event. *; <br/> public class testcancellayout extends frame {<br/> Public testcancellayout () {<br/> setlayout (null ); <br/> button b1 = new button ("first button"); <br/> button b2 = new button ("second button "); <br/> b1.setbounds (,); <br/> b2.setbounds (,); <br/> Add (B1); <br/> Add (B2 ); <br/>}< br/> Public static void main (string [] ARGs) {<br/> testcancellayout mainframe = new testcancellayout (); <br/> mainframe. setsize (300,200); <br/> mainframe. setvisible (true); <br/>}< br/>} 

 

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.