"Java Swing Discovery Road Series" Three: Java Swing layout Manager component __java

Source: Internet
Author: User

Author: Guo Jia
Email: allenwells@163.com
Blog: http://blog.csdn.net/allenwells
Github:https://github.com/allenwell a borderlayout

BorderLayout is a simple layout strategy that you can consider as a component. It divides the containers into 5 areas, east, south, west, north, and middle, each of which occupies an area. and
These 5 areas are named North, west, east, CENTER, SOUTH, and they are all defined as static
Constants. Static constants can be referenced directly, as follows: public static final String north = Northern of the entire content panel, the west of the entire content panel. Side public static final String Eastern = east of the entire content panel public static final Sning center = Middle public static f of the entire content panel of "center" inal String SOUTH = "SOUTH" south of the entire content panel

For Example 1

How to use the BorderLayout layout manager to lay out components.

import javax.swing.*; import java.awt.*; public class BorderLayoutDemo1 {static final int WI
    dth=300;
    static final int height=200;
         public static void Main (string[] args) {JFrame jf=new JFrame ("test program");
         Jf.setsize (Width,height);
         Jf.setdefaultcloseoperation (Jframe.exit_on_close);
         Jf.setvisible (TRUE);
         JPanel contentpane=new JPanel ();
         Jf.setcontentpane (ContentPane);
         JButton b1=new JButton ("Life");
         JButton b2=new JButton ("work");
         JButton b3=new JButton ("Sleep");
         JButton b4=new JButton ("shopping");
         JButton b5=new JButton ("diet");
         BorderLayout lay=new borderlayout ();//Create a Layout manager object that sets the intermediate container to the layout management jf.setlayout (lay);
         Contentpane.add (B1, "North"); the five normal button components are added to the intermediate container contentpane.add (B2, "South") respectively according to the east, south, west, north and five orientations;
         Contentpane.add (B3, "East");
         Contentpane.add (B4, "West");
    Contentpane.add (B5, "Center"); }  
}

The results of the operation are shown in the following illustration:

For Example 2

Add five content panels to five different orientations in the top-level container-related content panel, and each of the five content Panels contains button components in five different directions.

This code is intended primarily to show readers how to add five content panels to five different orientations in the top-level container-related content panel, and each of the five content panels contains a button component import javax.swing.* in five different directions;
Import java.awt.*;
    public class BorderLayoutDemo2 {static final int width=300;
    static final int height=200;
         public static void Main (string[] args) {JFrame jf=new JFrame ("test program");
         Jf.setsize (Width,height);
         Jf.setdefaultcloseoperation (Jframe.exit_on_close);
         Jf.setvisible (TRUE);
         JPanel contentpane=new JPanel ();
         Jf.setcontentpane (ContentPane);
         JButton b1=new JButton ("HKD");//created 25 normal button components JButton b2=new JButton ("RMB");
         JButton b3=new JButton ("USD");
         JButton b4=new JButton ("Euro");
         JButton b5=new JButton ("Pound sterling");
         JButton b6=new JButton ("motherboard");
         JButton b7=new JButton ("Memory");
         JButton b8=new JButton ("hard disk");
         JButton b9=new JButton ("Monitor");
         JButton b10=new JButton ("mouse");
         JButton b11=new JButton ("Rice");
  JButton b12=new JButton ("vegetable");       JButton b13=new JButton ("Rice");
         JButton b14=new JButton ("pork");
         JButton b15=new JButton ("beef");
         JButton b16=new JButton ("bread");
         JButton b17=new JButton ("cake");
         JButton b18=new JButton ("chocolate");
         JButton b19=new JButton ("cheese");
         JButton b20=new JButton ("apple pie");
         JButton b21=new JButton ("Notebook");
         JButton b22=new JButton ("Telephone");
         JButton b23=new JButton ("desk");
         JButton b24=new JButton ("pen");
         JButton b25=new JButton ("folder");
         Jf.setlayout (New BorderLayout ());
         The JPanel p1=new JPanel ();//creates five intermediate containers and sets their layout managers to BorderLayout.
         JPanel p2=new JPanel ();
         JPanel p3=new JPanel ();
         JPanel p4=new JPanel ();
         JPanel p5=new JPanel ();
         P1.setlayout (New BorderLayout ());
         P2.setlayout (New BorderLayout ());
         P3.setlayout (New BorderLayout ());
         P4.setlayout (New BorderLayout ());
         P5.setlayout (New BorderLayout ()); ConTentpane.add (P1, "North"); five intermediate container objects are added to the upper intermediate container separately and are arranged in a borderlayout manner Contentpane.add (P2, "South");
         Contentpane.add (P3, "East");
         Contentpane.add (P4, "West");
         Contentpane.add (P5, "Center");
         P1.add (B1, "North");///will be arranged from the first to the Fifth Normal button component in the P1 middle container p1.add (B2, "West") in the borderlayout manner;
         P1.add (B3, "South");
         P1.add (B4, "East");
         P1.add (B5, "Center");
         P2.add (B6, "North");//will be arranged from sixth to tenth normal button components in BorderLayout mode to P2 Intermediate container p2.add (B7, "West");
         P2.add (B8, "South");
         P2.add (B9, "East");
         P2.add (B10, "Center");
         P3.add (B11, "North");//will be arranged from 11th to 15th normal button components in BorderLayout mode to P3 intermediate container P3.add (B12, "West");
         P3.add (B13, "South");
         P3.add (B14, "East");
         P3.add (B15, "Center");
         P4.add (B16, "North");//will be arranged from 16th to 20th normal button components in BorderLayout mode to P4 intermediate container P4.add (B17, "West");
         P4.add (B18, "South");
         P4.add (B19, "East");
         P4.add (B20, "Center"); P5. Add (B21, "North");//will be borderlayout from the 21st to 25th Normal button assembly to the P5 Intermediate container p5.add (B22, "West");
         P5.add (B23, "South");
         P5.add (B24, "East");
         P5.add (B25, "Center");
 }  
}

The results of the operation are shown in the following illustration:

two FlowLayout

FlowLayout is arranged from left to right in the order in which the components are added, one row irresolute full, another row, and then continues from left to right, and the components of each row are arranged in the center.

For Example 1

How to use the FlowLayout layout manager

//This code is mainly for the reader to display the FlowLayout Layout Manager use method import javax.swing.*; import java.awt.*; public class
    FlowLayoutDemo1 {static final int width=300;
    static final int height=200;
         public static void Main (string[] args) {JFrame jf=new JFrame ("test program");
         Jf.setsize (Width,height);
         Jf.setdefaultcloseoperation (Jframe.exit_on_close);
         Jf.setvisible (TRUE);
         JPanel contentpane=new JPanel ();
         Jf.setcontentpane (ContentPane);
         JButton b1=new JButton ("HKD");
         JButton b2=new JButton ("RMB");
         JButton b3=new JButton ("USD");
         JButton b4=new JButton ("Euro");
         JButton b5=new JButton ("Pound sterling"); Contentpane.setlayout (New FlowLayout ());//sets the layout manager for the intermediate container to FlowLayout Contentpane.add (B1);
         Add five buttons to the intermediate container Contentpane.add (B2) According to the FlowLayout layout manager respectively;
         Contentpane.add (B3);
         Contentpane.add (B4);
         Contentpane.add (B5);
        Jf.pack (); }  
}

The effect is as shown in the following illustration:

For Example 2

Associate the FlowLayout layout manager with the top-level container, and then add the five Layout Manager content panel, which adds five components to each content panel, and each content panel arranges the components in BorderLayout layout management.

Import javax.swing.*;
Import java.awt.*;
    public class FlowLayoutDemo2 {static final int width=300;
    static final int height=200;
         public static void Main (string[] args) {JFrame jf=new JFrame ("test program");
         Jf.setsize (Width,height);
         Jf.setdefaultcloseoperation (Jframe.exit_on_close);
         Jf.setvisible (TRUE);
         JPanel contentpane=new JPanel ();
         Jf.setcontentpane (ContentPane);
         JButton b1=new JButton ("HKD");//created 25 normal button components JButton b2=new JButton ("RMB");
         JButton b3=new JButton ("USD");
         JButton b4=new JButton ("Euro");
         JButton b5=new JButton ("Pound sterling");
         JButton b6=new JButton ("motherboard");
         JButton b7=new JButton ("Memory");
         JButton b8=new JButton ("hard disk");
         JButton b9=new JButton ("Monitor");
         JButton b10=new JButton ("mouse");
         JButton b11=new JButton ("Rice");
         JButton b12=new JButton ("vegetable");
         JButton b13=new JButton ("Rice");
   JButton b14=new JButton ("pork");      JButton b15=new JButton ("beef");
         JButton b16=new JButton ("bread");
         JButton b17=new JButton ("cake");
         JButton b18=new JButton ("chocolate");
         JButton b19=new JButton ("cheese");
         JButton b20=new JButton ("apple pie");
         JButton b21=new JButton ("Notebook");
         JButton b22=new JButton ("Telephone");
         JButton b23=new JButton ("desk");
         JButton b24=new JButton ("pen");
         JButton b25=new JButton ("folder"); Contentpane.setlayout (New FlowLayout ());//Set the layout manager for the intermediate container to FlowLayout JPanel p1=new JPanel ();//create five intermediate containers, and the layout of each intermediate container
         The manager is set to BorderLayout JPanel p2=new JPanel ();
         JPanel p3=new JPanel ();
         JPanel p4=new JPanel ();
         JPanel p5=new JPanel ();
         P1.setlayout (New BorderLayout ());
         P2.setlayout (New BorderLayout ());
         P3.setlayout (New BorderLayout ());
         P4.setlayout (New BorderLayout ());
         P5.setlayout (New BorderLayout ()); Contentpane.add (p1); Add five intermediate containers to the upper intermediate container contEntpane.add (p2);
         Contentpane.add (p3);
         Contentpane.add (p4);
         Contentpane.add (p5);
         P1.add (B1, "North");//Add the first to fifth normal button to the P1 p1.add (B2, "West");
         P1.add (B3, "South");
         P1.add (B4, "East");
         P1.add (B5, "Center");
         P2.add (B6, "North");//Add sixth to tenth normal button to P2 P2.add (B7, "West");
         P2.add (B8, "South");
         P2.add (B9, "East");
         P2.add (B10, "Center");
         P3.add (B11, "North");//Add tenth to 15th normal button P3 P3.add (B12, "West");
         P3.add (B13, "South");
         P3.add (B14, "East");
         P3.add (B15, "Center");
         P4.add (B16, "North");//Add 16th to 20th normal button to P4 P4.add (B17, "West");
         P4.add (B18, "South");
         P4.add (B19, "East");
         P4.add (B20, "Center");
         P5.add (B21, "North");//Add 21st to 25th Normal button to P5 P5.add (B22, "West");
         P5.add (B23, "South");
         P5.add (B24, "East");
         P5.add (B25, "Center");
       Jf.pack ();
 }  
}

The effect is as shown in the following illustration:

three GridLayout

GridLayout divides the entire space into several rows of network regions, where the components are placed in these small areas.

For Example 1

Using the GridLayout layout manager, the nine normal button components are placed in the content panel in the program according to this layout manager.

Import javax.swing.*;
Import java.awt.*;
    public class GridLayoutDemo1 {static final int width=300;
    static final int height=200;
         public static void Main (string[] args) {JFrame jf=new JFrame ("test program");
         Jf.setsize (Width,height);
         Jf.setdefaultcloseoperation (Jframe.exit_on_close);
         Jf.setvisible (TRUE);
         JPanel contentpane=new JPanel ();
         Jf.setcontentpane (ContentPane);
         JButton b1=new JButton ("HKD");
         JButton b2=new JButton ("RMB");
         JButton b3=new JButton ("USD");
         JButton b4=new JButton ("Euro");
         JButton b5=new JButton ("Pound sterling");
         JButton b6=new JButton ("motherboard");
         JButton b7=new JButton ("Memory");
         JButton b8=new JButton ("hard disk");

         JButton b9=new JButton ("Monitor"); GridLayout gird=new GridLayout (3,3);

         Create a GridLayout Layout Manager object, set the number of rows to 3, set the number of columns to 3, and use it as the Layout manager contentpane.setlayout (gird) of the intermediate container; Contentpane.add (B1); Add nine normal button component one by one to an intermediate container ContentPane. Add (B2);
         Contentpane.add (B3);
         Contentpane.add (B4);
         Contentpane.add (B5);
         Contentpane.add (B6);
         Contentpane.add (B7);
         Contentpane.add (B8);
         Contentpane.add (B9);
      Jf.pack ();
 }  
}

The effect is as shown in the following illustration:

For Example 2

With the Girdlayout layout manager associated with the top-level window, add FlowLayout layout Manager and BorderLayout layout Manager to the layout manager, and finally add controls to these layout managers.

Import javax.swing.*;
Import java.awt.*;
    public class GridLayoutDemo2 {static final int width=300;
    static final int height=200;
         public static void Main (string[] args) {JFrame jf=new JFrame ("test program");
         Jf.setsize (Width,height);
         Jf.setdefaultcloseoperation (Jframe.exit_on_close);
         Jf.setvisible (TRUE);
         JPanel contentpane=new JPanel ();
         Jf.setcontentpane (ContentPane);
         JButton b1=new JButton ("HKD");//created 25 normal button components JButton b2=new JButton ("RMB");
         JButton b3=new JButton ("USD");
         JButton b4=new JButton ("Euro");
         JButton b5=new JButton ("Pound sterling");
         JButton b6=new JButton ("motherboard");
         JButton b7=new JButton ("Memory");
         JButton b8=new JButton ("hard disk");
         JButton b9=new JButton ("Monitor");
         JButton b10=new JButton ("mouse");
         JButton b11=new JButton ("Rice");
         JButton b12=new JButton ("vegetable");
         JButton b13=new JButton ("Rice"); JButton b14=new JButton ("Pork");
         JButton b15=new JButton ("beef");
         JButton b16=new JButton ("bread");
         JButton b17=new JButton ("cake");
         JButton b18=new JButton ("chocolate");
         JButton b19=new JButton ("cheese");
         JButton b20=new JButton ("apple pie");
         JButton b21=new JButton ("Notebook");
         JButton b22=new JButton ("Telephone");
         JButton b23=new JButton ("desk");
         JButton b24=new JButton ("pen");
         JButton b25=new JButton ("folder");
         GridLayout gird=new GridLayout (3,3);
         Jf.setlayout (gird); JPanel p1=new JPanel (), create five intermediate containers, and set the layout of the first and second, fourth, and fifth intermediate containers to borderlayout, and the third to FlowLayout, JPanel p2=new
         El ();
         JPanel p3=new JPanel ();
         JPanel p4=new JPanel ();
         JPanel p5=new JPanel ();
         P1.setlayout (New BorderLayout ());
         P2.setlayout (New BorderLayout ());
         P3.setlayout (New FlowLayout ());
         P4.setlayout (New BorderLayout ());
         P5.setlayout (New BorderLayout ()); Contentpane.add (p1); Add five intermediate containers to the Outer intermediate container Contentpane.add (p2);
         Contentpane.add (p3);
         Contentpane.add (p4);
         Contentpane.add (p5);
         P1.add (B1, "North");//Add the first component to the fifth component to the P1 P1.add (B2, "West");
         P1.add (B3, "South");
         P1.add (B4, "East");
         P1.add (B5, "Center");
         P2.add (B6, "North");//Add Sixth component to tenth component to P2 P2.add (B7, "West");
         P2.add (B8, "South"); P2.add (B9, "East"
Related Article

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.