Turn: Java graphical interface Design--layout manager's FlowLayout (flow layout) Other please refer to the reproduced source URL

Source: Internet
Author: User

http://blog.csdn.net/liujun13579/article/details/7771191

The previous article explains JFrame, JPanel, which already involves the use of empty layouts. While Java can pinpoint components in pixels, there will be some display differences in different systems, so that the display is not the same, so Java provides a layout manager to make the graphical interface written with good platform independence.

Note: All layout managers are used for containers, including top-level containers and intermediate containers.

One, the layout manager belongs to the class package

Owning class Package

Layout Manager Name

Description

java.awt

FlowLayout (Flow layout)

The components are aligned from left to right according to the order in which they are joined, and a row is filled to the next line to begin the arrangement

BorderLayout (Border layout)

Containers are divided into five regions, east, west, south, north, and Central, where only one component can be placed.

GridLayout (Grid layout)

The container's space is divided into a grid area of the MXN column, where only one component can be placed per region.

CardLayout (Card layout)

Like a stack of cards, each card corresponds to one component, but only one card at a time can be displayed. For situations where multiple components are prevented in one space

GridBagLayout (Grid package layout)

GridLayout, components are still placed in rows and columns, but each component can occupy multiple meshes

Java.swing

BoxLayout (Box-type layout)

Allows multiple controls to be prevented vertically or horizontally in the container

Spriglayout (Spring Layout)

Placing controls based on a set of constraints

No

Empty layout

Place controls by the size, location information provided by the control itself, without using the layout manager

Second, the container's default layout manager

Each container has a default layout management, as shown in the following table:

Container

Default layout mode

Top-level containers

JFrame

BorderLayout (Border layout)

JDialog

BorderLayout (Border layout)

JApplet

FlowLayout (Flow layout)

Intermediate container

JPanel

FlowLayout (Flow layout)

Three, FlowLayout (flow-type layout)

The components in a container using the FlowLayout layout are arranged in the order in which they are joined (centered, left, right, aligned) from left to right, with a row full (that is, after the component exceeds the container width) to the next line to begin the arrangement.

1, the flow layout features are as follows:

L components are arranged according to the alignment of the set

L Regardless of alignment, the components are arranged from left to right, one row full, and the next line is transferred. (for example, in the right-aligned arrangement, the first component at the far right of the first line, the second component is added, the first component moves to pan left, the second component becomes the rightmost component of the line, which is the left-to-right arrangement)

2. Flow layout common constructors and methods of FlowLayout class

constructor function

Name

Use

FlowLayout ()

Constructs a new flowlayout, which is the default center-aligned, with the default horizontal and vertical clearance being 5 pixels

flowlayout (int align)

construct a new flowlayout with the specified alignment, the default horizontal and vertical clearance is 5 pixels

five parameter values and meanings are as follows:

0 or flowlayout.left, control left justified

1 or Flowlayout.center, center-align

2 or flowlayout.right, right-justified

Span style= "FONT-SIZE:18PX;" >3 or flowlayout.leading, the control corresponds to the start edge of the container orientation

FlowLayout (int align, int hgap, int vgap)

Creates a new flow layout manager that has the specified alignment as well as the specified horizontal and vertical clearances.

Method

Name

Use

Void setalignment (int align)

Sets the alignment for this layout.

void Sethgap (int hgap)

Sets the horizontal gap between components and the edges of the component and the Container.

void Setvgap (int vgap)

Sets the vertical gap between components and the edges of the component and the Container.

3. FlowLayout Layout Application Code Snippet Example

1) Set FlowLayout layout

JFrame fr=new JFrame ();

FlowLayout flow=new FlowLayout ();

Fr.setlayout (flow);

The above statement can be simplified into:
Fr.setlayout (New FlowLayout ());

2) Set the FlowLayout layout of the frame fr to the left alignment of the component

Fr.setlayout (Newflowlayout (flowlayout.left));

3) Set the frame fr to the left-aligned flowlayout layout of the component, and the component's horizontal spacing is 20 pixels and the vertical spacing is 40 pixels.

Fr.setlayout (New FlowLayout (flowlayout.left,20,40));

Example: Alignment

Flowlayoutdemo.java

importjavax.swing.*;

importjava.awt.*;

Public Classflowlayoutdemo extends JFrame {

Public Flowlayoutdemo () {

Set the form as a streaming layout with no parameters default to center-aligned

SetLayout (New FlowLayout ());

Set the font style that is displayed in the form

SetFont (New Font ("Helvetica", Font.plain, 14));

Add a button to a form

Getcontentpane (). Add (Newjbutton ("button 1"));

Getcontentpane (). Add (New JButton ("button 2"));

Getcontentpane (). Add (New JButton ("Button3"));

Getcontentpane (). Add (Newjbutton ("button 4"));

}

public static void Main (String args[]) {

Flowlayoutdemo window = Newflowlayoutdemo ();

Window.settitle ("Flow-style layout");

The code sets the size of the window to fit all the components you place

Window.pack ();

Window.setvisible (TRUE);

Window.setdefaultcloseoperation (Jframe.exit_on_close);

Window.setlocationrelativeto (NULL); To center the form on the display

}

}

As shown in the program execution results, a center-displayed form is generated with four buttons on it, and the buttons and the left and right edges of the form are the default 5-pixel spacing. Change the size of the form as shown in the original interface, widen the original interface, narrow the original interface, and the spacing between the components and the interface.

Modify the program code experience the interface layout effect:

SetLayout (Newflowlayout ());

Change the code in the above source program as follows, and then make the following changes:

SetLayout (newflowlayout (0)); Component left Justified

SetLayout (Newflowlayout (flowlayout.right,10,15)); Component right-aligned, horizontal spacing between components is 10 pixels, vertical spacing is 15 pixels

Go: Java Graphical interface design-layout manager flowlayout (flow layout) Other please refer to the reprint URL

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.