Five common la s of Swing and wing in java

Source: Internet
Author: User

Five common la s of Swing and wing in java

1. Border layout (BorderLayout)

2. FlowLayout)

3. GridLayout)

4. BoxLaYout)

5. Empty layout (null)

There are two other layout S: GridBagLayout (grid package layout) and CardLayout (card layout)

Note: The default layout of JFrame and JDialog is BorderLayout, while that of JPanel and Applet is FlowLayout.

Sample Code for border layout:

Import java. awt. borderLayout; import javax. swing. JButton; import javax. swing. JFrame; public class BorderLayoutExample extends JFrame {JButton btn1 = new JButton ("East"); JButton btn2 = new JButton ("South "); JButton btn3 = new JButton ("West"); JButton btn4 = new JButton ("North"); JButton btn5 = new JButton ("medium"); BorderLayoutExample () {init (); this. setTitle ("border layout"); this. setResizable (true); this. setSize (200,200); this. setLocationRelativeTo (null); this. setdefaclocloseoperation (EXIT_ON_CLOSE); this. setVisible (true);} void init () {this. setLayout (new BorderLayout (); // default value:; Horizontal spacing: 10; Vertical spacing: 5 this. add (btn1, BorderLayout. EAST); this. add (btn2, BorderLayout. SOUTH); this. add (btn3, BorderLayout. WEST); this. add (btn4, BorderLayout. NORTH); this. add (btn5, BorderLayout. CENTER) ;}public static void main (String args []) {new BorderLayoutExample ();}}

Running result:

Sample Code for stream layout:

Import java. awt. flowLayout; import javax. swing. JButton; import javax. swing. JFrame; public class FlowLayoutExample extends JFrame {JButton btn1 = new JButton ("one"); JButton btn2 = new JButton ("two "); JButton btn3 = new JButton ("three"); JButton btn4 = new JButton ("four"); JButton btn5 = new JButton ("five"); FlowLayoutExample () {init (); this. setTitle ("stream layout"); this. setResizable (true); this. setSize (200,200); this. setLocationRelativeTo (null); this. setdefaclocloseoperation (EXIT_ON_CLOSE); this. setVisible (true);} void init () {this. setLayout (new FlowLayout (FlowLayout. LEFT,); // The default value is center; Horizontal spacing is 10, Vertical spacing is 5 this. add (btn1); this. add (btn2); this. add (btn3); this. add (btn4); this. add (btn5);} public static void main (String args []) {new FlowLayoutExample ();}}

Running result:

Grid layout sample code:

Import java. awt. gridLayout; import javax. swing. JButton; import javax. swing. JFrame; public class GridLayoutExample extends JFrame {JButton btn1 = new JButton ("one"); JButton btn2 = new JButton ("two "); JButton btn3 = new JButton ("three"); JButton btn4 = new JButton ("four"); JButton btn5 = new JButton ("five"); GridLayoutExample () {init (); this. setTitle ("table layout"); this. setResizable (true); this. setSize (300,200); this. setLocationRelativeTo (null); this. setdefaclocloseoperation (EXIT_ON_CLOSE); this. setVisible (true);} void init () {this. setLayout (new GridLayout (,); // The default value is 1 row and n column; 2 rows and 3 columns, with a horizontal spacing of 10 and a Vertical spacing of 5this. add (btn1); this. add (btn2); this. add (btn3); this. add (btn4); this. add (btn5);} public static void main (String args []) {new GridLayoutExample ();}}

Running result:

Sample Code for Box layout:

Import javax. swing. box; import javax. swing. boxLayout; import javax. swing. JButton; import javax. swing. JFrame; public class BoxLaYoutExample extends JFrame {JButton btn1 = new JButton ("one"); JButton btn2 = new JButton ("two "); JButton btn3 = new JButton ("three"); JButton btn4 = new JButton ("four"); JButton btn5 = new JButton ("five"); BoxLaYoutExample () {init (); this. setTitle ("table layout"); this. setResizable (true); this. setSize (300,200); this. setLocationRelativeTo (null); this. setdefaclocloseoperation (EXIT_ON_CLOSE); this. setVisible (true);} void init () {this. setLayout (new BoxLayout (this. getContentPane (), BoxLayout. x_AXIS); // you can use the Box container instead of/Box box Box = new Box (BoxLayout. y_AXIS); box. add (btn ...); box. add (creat ..); this. add (btn1); this. add (btn2); this. getContentPane (). add (Box. createHorizontalStrut (10); // when the x layout is used, add a fixed-width component to separate it. // this. getContentPane (). add (Box. createVerticalStrut (5); // when y is used, add a fixed height component to separate this. add (btn3); this. add (btn4); this. add (btn5);} public static void main (String args []) {new BoxLaYoutExample ();}}

Running result:

Sample Code for empty layout:

Import javax. swing. JButton; import javax. swing. JFrame; public class NullLayoutExample extends JFrame {JButton btn1 = new JButton ("one"); JButton btn2 = new JButton ("two "); JButton btn3 = new JButton ("three"); JButton btn4 = new JButton ("four"); JButton btn5 = new JButton ("five"); NullLayoutExample () {init (); this. setTitle ("Empty layout"); this. setResizable (true); this. setSize (300,300); this. setLocationRelativeTo (null); this. setdefaclocloseoperation (EXIT_ON_CLOSE); this. setVisible (true);} void init () {this. setLayout (null); btn1.setBounds (10, 0,100, 50); // x coordinate 10, y coordinate 0, component width 100, height 50btn2. setBounds (20, 50,100, 50); btn3.setBounds (30,100,100, 50); btn4.setBounds (40,150,100, 50); btn5.setBounds (50,200,100, 50); this. add (btn1); this. add (btn2); this. add (btn3); this. add (btn4); this. add (btn5);} public static void main (String args []) {new NullLayoutExample ();}}

Running result:

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.