JAVA three basic layout manager

Source: Internet
Author: User

1.FlowLayout

The simplest layout manager, using the framework, arranges components from left to right in the container, in the order in which they are added.

When a row is full, start a new line. You can use three constants (to specify the alignment of a build):

Flowlayout.right

Flowlayout.center

Flowlayout.left

Package Chapter12;import Javax.swing.jframe;import Java.awt.flowlayout;import javax.swing.jlabel;//import javax.swing.jtextfield;//Pixels long @suppresswarnings ("Serial") public class Showflowlayout extends Jframe{public Showflowlayout () {setlayout (new FlowLayout (flowlayout.right,10,20));/*  equivalent to the    *  comment section created  The display reference to the FlowLayout object layout    is not necessary because the object is not directly referenced in the Showflowlayout class flowlayout layout = new FlowLayout ( flowlayout.right,10,20), setlayout (layout), */add (New JLabel ("First Name"), add (new JTextField), add (New JLabel (" MI "), Add (new JTextField (1)), add (New JLabel (" Last Name "), Add (new JTextField (8));} public static void Main (string[] args) {showflowlayout frame = new Showflowlayout (); Frame.settitle ("showflowlayout"); Frame.setsize (+); Frame.setlocationrelativeto (null); Frame.setdefaultcloseoperation (Jframe.exit_on_close); Frame.setvisible (True);}}




2.GridLayout Manager

/*
* Manage components in the form of a grid.
* Components are arranged from left to right in the order in which they are added.
* The number of rows or columns can be 0 but not all 0
* If a row or column that is 0 other than 0 is not 0, the rows or columns that have been fixed to 0 are determined dynamically by the layout manager.
* Ex: a grid of 10 components with 0 rows and 3 columns GridLayout assigns 3 fixed columns and 4 rows to the last row containing only one column
* If neither is 0 the behavior of the dominant parameter row is that the fixed column is dynamically allocated

* If you change the size of this frame, the layout of the button remains unchanged
*/

Package chapter12;/* * Manages components in the form of a grid. * Components are arranged from left to right in the order in which they are added. * The number of rows or columns can be 0, but not all 0 * If a row or column that is 0 of the other not 0 and not 0 is already fixed to 0, the row or column is determined dynamically by the layout manager. * Ex: A grid with 0 rows and 3 columns of 10 components GridLayout assigns it 3 fixed columns and 4 rows The last row contains only one column * If none is 0 then the behavior of the dominant parameter row is a fixed column that is dynamically assigned */import Javax.swing.JLabe L;import javax.swing.jtextfield;import Javax.swing.jframe;import Java.awt.gridlayout;public class ShowGridLayout Extends JFrame {public showgridlayout () {//Set GridLayout, 3 rows, 2 columns, and gaps 5 between//components H    Orizontally and vertically setlayout (new GridLayout (3, 2, 5, 5));    Add labels and text fields to the frame add (new JLabel ("First Name"));    Add (New JTextField (8));    Add (New JLabel ("MI"));    Add (New JTextField (1));    Add (New JLabel ("Last Name"));  Add (New JTextField (8));    } public static void Main (string[] args) {showgridlayout frame = new Showgridlayout ();    Frame.settitle ("Showgridlayout");    Frame.setsize (200, 125); Frame.setlocationrelativeto (NULL); Center the frame FramE.setdefaultcloseoperation (Jframe.exit_on_close);  Frame.setvisible (TRUE); }}




3.borderlayout


Package chapter12;/* * BorderLayout divides a container into five zones */import Java.awt.borderlayout;import javax.swing.jbutton;import Javax.swing.JFrame, @SuppressWarnings ("Serial") public class Showborderlayout extends Jframe{public showborderlayout ( {setlayout (new BorderLayout (5,10)), Add (New JButton ("EAST"), Borderlayout.east), add (New JButton ("South"), Borderlayout.south), Add (New JButton ("WEST"), Borderlayout.west), add (New JButton ("North"), Borderlayout.north), add ( New JButton ("center"), Borderlayout.center);} public static void Main (string[] args) {showborderlayout frame = new Showborderlayout (); Frame.settitle (" Showborderlayout "); Frame.setsize (300,200); Frame.setlocationrelativeto (null); Frame.setdefaultcloseoperation ( Jframe.exit_on_close); frame.setvisible (True);}}





JAVA three basic layout manager

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.