Java Layout Learning (i)

Source: Internet
Author: User

Java programs can be well ported to other platforms through the JVM, but Java-generated graphical interface styles often need to be resized without the use of layouts to adjust to the best style on the new platform. This is because the optimal size of the component is often platform-dependent. On different platforms, the same content size may not be the same. Java specifically provides LayoutManager (layout manager) for better layout management, so that the size and position of the component can be optimally adjusted. (includes no occlusion of controls, no redundant space).

(a) before starting to explain the layout, let's first popularize some common knowledge about the layout.

1, the container Container has done the graphical interface drawing work The person basic knows this container concept. If the first contact, you can simply understand him as a panel, you can draw a control on the thing. Common container include: Window panel ScrollPane where window also includes frame (form) Dialog (dialog box)

2, the layout of the container set Container.setlayoutmanager (New LayoutManager ());

(ii) Java is commonly used in the 5+1+1 type of layout.

1) of which 5 represents the AWT layout of 5 are FlowLayout, BorderLayout, GridLayout, GridBagLayout, cardlayout below in turn to introduce each layout

(1) Fowlayout flow layout

What is flow layout, flow represents flow, Fowlayout is (anti-theft connection: This article starts from the http://www.cnblogs.com/jilodream/) control is like a stream (queue) to a certain arrangement, when the boundary is encountered, Just retrace the line from the next line. Just like the military training when the people row by row of the station, when a row of people full, on another line to continue to line up.

By default, the Fowlayout layout Manager arranges all components from left to right.

Styles such as

There are three types of common constructors

Fowlayout ()

Fowlayout (int align)

Fowlayout (int align, int hgap, int vgap)

Parameter explanation:

1, where align represents the alignment, including three, the value as a static constant is stored in the FlowLayout

Flowlayout.left align from left to right

Flowlayout.right align from right to left

Flowlayout.center align from center to side

The first two are similar to the left side of military training, to the right, the last kind of photography, in the middle of each row as a benchmark, both sides toward the middle.

2, Hgap, the horizontal spacing between the controls PS. Note that the spacing here and below refers to the horizontal and vertical distances of any control with its surrounding control controls

3, Vgap, vertical spacing between controls

1 Importjava.awt.FlowLayout; 2 ImportJavax.swing.JFrame; 3 ImportJavax.swing.JButton; 4    5    6  Public classFlowlayoutdemo7 {   8      Public Static voidMain (string[] args)9     {   TenJFrame f=NewJFrame ("FlowLayout");  OneF.setlayout (NewFlowLayout ());  A          for(inti=0;i<7;i++) -         {    -JButton btn=NewJButton ("button" +i);  the F.add (BTN);  -         }    -F.setsize (300,150);  -F.setvisible (true);  + f.setdefaultcloseoperation (jframe.exit_on_close);  -     }    +}

Display effect:

2) borderlayout Border layout

Bordern N. Border; border borderlayout that is to divide the container through the boundary into several regions, each of which is always present and the number is always 5: East, West, south, north, middle (i.e. East/west/south/north/center ), the relative position of the area as. The added control can be specified anywhere in the zone.

This layout has four points to note

1, when the layout of the size of the adjustment north/south/center to adjust the horizontal east/west/center vertical adjustment such as widen form, then only north/south/center the width of the three areas will be larger, The width of West and east does not change

2. When adding controls to a container in a borderlayout layout, you need to specify the control (anti-theft connection: This article starts from the http://www.cnblogs.com/jilodream/) in which area to place the piece. Otherwise, the system method places the control in the center area by default.

3. You can add only one control or one container per zone. If added repeatedly, subsequent controls will overwrite the control that was added.

4, Frame, Dialog, ScrollPane is the default use of this layout, so directly to these containers to add controls, and eventually only display a control

There are two common types of constructors

BorderLayout ()

BorderLayout (int hgap, int vgap)

PS. hgap, horizontal spacing between regions. Vgap, the vertical spacing between regions.

1 Importjava.awt.BorderLayout; 2 ImportJavax.swing.JFrame; 3 ImportJavax.swing.JButton; 4    5    6  Public classBorderlayoutdemo7 {   8      Public Static voidMain (string[] args)9     {   TenJFrame f=NewJFrame ("BorderLayout");  OneJButton btn=NewJButton ("Borderlayout.north");  AF.add (Btn,borderlayout.north);//North , South, EAST, WEST, center are set as static variables in BorderLayout -btn=NewJButton ("Borderlayout.south");  - F.add (Btn,borderlayout.south);  thebtn=NewJButton ("Borderlayout.east");  - F.add (btn,borderlayout.east);  -btn=NewJButton ("Borderlayout.west");  - F.add (btn,borderlayout.west);  +btn=NewJButton ("Borderlayout.center");  - F.add (Btn,borderlayout.center);  + F.pack ();  AF.setvisible (true);  at f.setdefaultcloseoperation (jframe.exit_on_close);  -     }    -}

Java Layout Learning (i)

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.