GUI layout: Boundary layout, flow layout, grid layout, card layout

Source: Internet
Author: User

Boundary layout


Package Guitest;//jframe default is the boundary layout borderlayoutimport Java.awt.borderlayout;import Javax.swing.jbutton;import Javax.swing.jframe;public class Borderlayoutdemo {public static void main (string[] args) {JFrame f = new JFrame ("Boundary layout Borde Rlayout ");//jframe default LayoutManager is Borderlayout//f.setlayout (new BorderLayout ());//Can not write, the default is flow layout JButton btn =  New JButton ("North"); F.add (Btn,borderlayout.north); Btn=new JButton ("South"); F.add (Btn,borderlayout.south); btn=new JButton ("East"); F.add (btn,borderlayout.east); Btn=new JButton ("West"); F.add (btn,borderlayout.west); Btn=new JButton ("Medium") ; F.add (Btn,borderlayout.center); F.pack ();//can also be set with F.setsize (222,222); f.setvisible (true); F.setdefaultcloseoperation (Jframe.exit_on_close);}}

Flow layout


Package Guitest;import Java.awt.borderlayout;import Java.awt.cardlayout;import java.awt.event.actionevent;import Java.awt.event.actionlistener;import Javax.swing.jbutton;import Javax.swing.jframe;import Javax.swing.JLabel; Import Javax.swing.jpanel;public class Cardlayoutdemo {private static JPanel p;public static void Main (string[] args) {JFr Ame f = new JFrame ("card layout cardlayout");p =new JPanel ();//set P layout Manager for card layout cardlayoutp.setlayout (New CardLayout ());// New two Jpaneljpanel p1 = new JPanel (); JPanel P2 = new JPanel (); JLabel lb = new JLabel ("first panel");p 1.add (LB),///panel with label Lb=new JLabel ("second panel");p 2.add (lb);//Panel with tag// Add the newly created two JPanel panels to P p.add (P1, "first");p. Add (P2, "second"), or//set to display the JPanel P1 ((cardlayout) p.getlayout () corresponding to first for first. Show (P, "first"); JButton btn = new JButton ("Change panel"), Btn.addactionlistener (new ActionListener () {@Overridepublic void actionperformed ( ActionEvent e) {//TODO auto-generated method stub//When you click ' Change Panel ', the second corresponding JPanel P2 ((cardlayout) p.getlayout ()) is displayed. Show (P , "Second");}); F.add (Btn,borderLayout.north); F.add (P,borderlayout.center); f.setsize (400,150);//f.pack (); f.setvisible (true); F.setdefaultcloseoperation (Jframe.exit_on_close);}}

Grid layout


Package Guitest;import Java.awt.flowlayout;import Javax.swing.jbutton;import Javax.swing.jframe;public class Flowlayoutdemo {public static void main (string[] args) {//TODO auto-generated method Stubjframe f = new JFrame ("Streaming layout flowl Ayout "), F.setlayout (New FlowLayout ()), for (int i=0;i<7;i++) {JButton btn=new JButton (" button "+i); F.add (BTN);} F.setsize (300,250);//f.pack (); default border set width and length just like f.setvisible (true); F.setdefaultcloseoperation (Jframe.exit_on_ CLOSE);}}

Card layout


Package Guitest;import Java.awt.gridlayout;import Javax.swing.jbutton;import Javax.swing.jframe;public class Girdlayoutdemo {public static void main (string[] args) {JFrame f = new JFrame ("Grid layout girdlayout");// Set the layout manager for F to 3 rows of 3 columns with horizontal and vertical spacing between girdlayout components 5f.setlayout (new GridLayout (3,3,5,5)), and for (int i=1;i<10;++i) {JButton btn = New JButton (string.valueof (i)); F.add (BTN);} F.pack (); f.setvisible (true); F.setdefaultcloseoperation (Jframe.exit_on_close);}}



GUI layout: Boundary layout, flow layout, grid layout, card layout

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.