Java layout Learning (1): java layout Learning

Source: Internet
Author: User

Java layout Learning (1): java layout Learning

Java programs can be well transplanted to other platforms through jvm, but the graphic interface style generated by java usually needs to be reset when layout is not used, in order to adjust the best style on the new platform. This is because the optimal size of components is usually related to the platform. On different platforms, the size of the same content may be different. java provides LayoutManager (layout manager) for better layout management, so that the size and position of components can be adjusted to the best. (Including controls that are unobstructed and have no redundant space ).

(1) Before explaining the layout, I would like to popularize some common knowledge about the layout.

1. Anyone who has worked on graphic interface drawing of Container iner knows the concept of Container. For the first contact, you can simply regard it as a panel and draw controls on it. Commonly used Container includes: window panel scrollPane. Window also includes Frame (form) Dialog (Dialog box)

2. Set Container. setLayoutManager (new layoutManager () for the Container layout ());

 

(2) Java commonly uses 5 + 1 + 1 la S.

1) Five of them represent Five layout, BorderLayout, GridLayout, GridBagLayout, and CardLayout respectively.

(1) FowLayout stream Layout

What is the flow layout, flow represents the flow of water, FowLayout that is (anti-theft connection: the first self-http://www.cnblogs.com/jilodream/) control is like a flow (Queue) to a certain arrangement, when there is a boundary, it is converted back to continue queuing from the next line. It is like standing in a row of personnel during military training. When the number of people in a row is full, another row will continue to be lined up.

By default, the FowLayout layout manager arranges all components from left to right.

Style as shown in

There are three types of common Constructors

FowLayout ()

FowLayout (int align)

FowLayout (int align, int hgap, int vgap)

Parameter description:

1. align indicates alignment, which includes three types. Values are saved as static constants in FlowLayout.

FlowLayout. LEFT Align from LEFT to right

FlowLayout. RIGHT align from RIGHT to left

FlowLayout. CENTER Align from CENTER to both sides

The first two types of military training are similar to the left-to-right and the right-to-right. The last one is similar to the photography, with the middle of each row as the benchmark, and the two sides move closer to the middle.

2. Horizontal spacing between controls in hgap ps. Note that the spacing here and below refers to the horizontal and vertical distance between any control and its peripheral controls.

3. vgap: Vertical spacing between controls

 1 import java.awt.FlowLayout;    2 import javax.swing.JFrame;    3 import javax.swing.JButton;    4     5     6 public class FlowLayoutDemo 7 {    8     public static void main(String[] args) 9     {   10         JFrame f=new JFrame("FlowLayout");   11         f.setLayout(new FlowLayout());   12         for(int i=0;i<7;i++)13         {   14             JButton btn=new JButton("Button"+i);   15             f.add(btn);   16         }   17         f.setSize(300,150);   18         f.setVisible(true);   19         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   20     }   21 }

Display Effect:

2) BorderLayout border Layout

Bordern n. border, border, and border BorderLayout, that is, the container is divided into several regions through the border. These regions always exist and the number is always five: EAST, WEST, SOUTH, NORTH, and middle (that is, EAST/WEST/SOUTH/NORTH/CENTER). The relative location of the region is as follows. the added control can be specified to any location in the region.

This layout has four points to note

1. When the layout size is adjusted, NORTH/SOUTH/CENTER horizontally adjusts EAST/WEST/CENTER vertically, for example, pulling a wide form, the width of the three regions NORTH/SOUTH/CENTER will increase, and the width of WEST and EAST will not change.

2. When adding controls to the BorderLayout layout container, You need to specify the control (anti-theft connection: the first self-http://www.cnblogs.com/jilodream/) parts to be placed in which area. Otherwise, the system method places the control in the center area by default.

3. Only one control or container can be added to each region. If you add Multiple widgets, the widgets that you add later will overwrite the widgets that you add before.

4. Frame, Dialog, and ScrollPane use this layout by default. Therefore, add controls directly to these containers and only one control is displayed.

There are two common constructor types:

BorderLayout ()

BorderLayout (int hgap, int vgap)

Ps. hgap, horizontal spacing between regions. Vgap, Vertical spacing between regions.

1 import java. awt. borderLayout; 2 import javax. swing. JFrame; 3 import javax. swing. JButton; 4 5 6 public class BorderLayoutDemo 7 {8 public static void main (String [] args) 9 {10 JFrame f = new JFrame ("BorderLayout "); 11 JButton btn = new JButton ("BorderLayout. NORTH "); 12 f. add (btn, BorderLayout. NORTH); // In BorderLayout, the NORTH, SOUTH, EAST, WEST, and CENTER are set as static variables 13 btn = new JButton ("BorderLayout. SOUTH "); 14 f. add (btn, BorderLayout. SOUTH); 15 btn = new JButton ("BorderLayout. EAST "); 16 f. add (btn, BorderLayout. EAST); 17 btn = new JButton ("BorderLayout. west "); 18 f. add (btn, BorderLayout. WEST); 19 btn = new JButton ("BorderLayout. CENTER "); 20 f. add (btn, BorderLayout. CENTER); 21 f. pack (); 22 f. setVisible (true); 23 f. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); 24} 25}

 

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.