Javase learning the layout manager of 55:gui programming

Source: Internet
Author: User

a Layout manager Overview

In the Java language, objects that provide a layout manager class can be managed.

Manages the layout of the Component object in the container object without having to set the Component object position and size directly. Each Container object

all have a layout management object, when the container needs to be specific to a component or determine its size, the corresponding layout manager is called,

with the container object's setlayout () method Change change its layout manager object.

AWT provides 5 layout managers, namely:

FlowLayout Layout Manager

BorderLayout Layout Manager

GridLayout layout Manager

CardLayout Layout Manager

GridBagLayout Layout Manager

The top three layout managers are most commonly used, so I'll go over these three layout managers in more detail.

two FlowLayout layout manager

FlowLayout is the default layout manager for the Panel class. the FlowLayout layout manager positions the components line-by-row, left-to-right in rows, and after a row is filled

line break. You can set different component spacing, line spacing, and alignment by displaying components in their original dimensions without changing the size of the component .

FlowLayout layout Manager The default alignment is centered.

A flow layout is used to schedule a component in a forward flow, which is very similar to a line of text in a paragraph. The direction of the flow depends on the componentorientation of the container

property, which may be one of the following two values: Componentorientation.left_to_right and

Componentorientation.right_to_left.

Flow layouts are typically used to arrange buttons in a panel. It places the button horizontally until there is no more suitable button on the same line. The pair aligns of the line

Type is determined by the Align property. Possible values are: Left, right, CENTER, leading, TRAILING.

fields of the FlowLayout class:


How to construct the FlowLayout class:


We can cite a few examples:


methods of the FlowLayout class:


Instance code:

Testflowlayout.java Source code:

Import java.awt.*;/* Example Name: FlowLayout Usage Example * source file name: Testflowlayout.java * to  point: * *. Layout manager concept and role *   2. The nature and usage of FlowLayout */public class testflowlayout{public    static void Main (String args[]) {frame f = new Frame ("Flow Layo UT ");        Button button1 = New button ("OK");        Button button2 = New button ("Open");        Button Button3 = New button ("Close");        F.setlayout (New FlowLayout (Flowlayout.left));        F.add (button1);        F.add (button2);        F.add (Button3);        F.setsize (100,100);        F.setvisible (True);}    }

Operation Result:


Testflowlayout2.java Source Code :

Import java.awt.*;p ublic class testflowlayout2{public    static void Main (String args[]) {        frame F = new Frame ("Java Form ");        FlowLayout L = new FlowLayout (Flowlayout.center, +, +);        F.setlayout (l);         F.setlocation (300,400);        F.setsize (300,200);        F.setbackground (New Color (204,204,255));        for (int i = 1; i<=7; i++) {            f.add (New button ("button"));        }        F.setvisible (True);}    }

Operation Result:


three borderlayout layout manager

The BorderLayout layout Manager is the default layout manager for the frame class.

The BorderLayout layout manager divides the layout of the entire container into East (east), west, south, north,

(CENTER) five districts domain, the component can only be added to the specified zone. If you do not specify the part of the component, the center area is added by default. each District

domain can only join one group If you add more than one, the previous join will be overwritten.

This is a layout of the container's border, which arranges the container components and resizes them to conform to the following five areas: North, South,

East, west, middle. Each zone can contain at most one component and is identified by the corresponding constants: North, South, EAST, WEST,

CENTER. When you use a border layout to add a component to a container, use one of these five constants, for example:

panel p = new Panel ();p. setlayout (New BorderLayout ());p. Add (New button ("Okay"), Borderlayout.south);

For convenience, BorderLayout interprets the absence of a string description as a constant center:

panel P2 = new Panel ();p 2.setLayout (New BorderLayout ());p 2.add (New TextArea ());  Same as P.add (new TextArea (), borderlayout.center);

BorderLayout Layout Manager Dimension Scaling principle:

1) north and South two regions in the horizontal direction of scaling;

2) east and West two regions in the vertical method of scaling;

3) The central area is scaled in two directions.

The component is laid out according to its preferred size and container size constraints (constraints). North and South components can be stretched in a horizontal direction;

The East and West components can be stretched vertically, and the center component can be stretched both horizontally and vertically to fill all remaining Empty

Room

fields of the BorderLayout class:


How to construct the BorderLayout class:


methods of the BorderLayout class:




Instance Code :

Import java.awt.*;/* Example Name: BorderLayout Application Example * source file name: Testborderlayout.java * to  point: BorderLayout layout Manager's nature and usage */public Class Testborderlayout{public static void Main (String args[]) {frame f = new Frame ("Border Layout"); Button bn = New Button ("bn"); Button bs = new button ("BS"); Button bw = New button ("BW"); button is = New button ("be"); button BC = New button ("BC");                F.add (BN, Borderlayout.north); F.add (BS, Borderlayout.south); F.add (BW, borderlayout.west); F.add (BE, Borderlayout.east); F.add (BC, Borderlayout.center);//may also use the following statement, but it is not recommended to use/*f.add (bn, "North"); F.add (BS, "South"); F.add (BW, "West"); F.add (BE, "East"); F.add (BC, "Center"); */f.setsize (400,300); f.setvisible (True);}}

Operation Result:


four GridLayout layout manager

The GridLayout class is a layout processor that lays out the components of a container in a rectangular grid. The container is divided into rectangles of equal size, a

Place a component in the rectangle.

The GridLayout layout manager divides the space into a rectangular grid of rules, each with an equal size. Components are added to each cell, first

Add a line from left to right, then wrap, and then top to bottom. In the GridLayout constructor method, specify the number of rows and columns to split, such as: GridLayout (3,4).

How to construct the GridLayout class:


methods of the GridLayout class:


Instance Code :

Import java.awt.*;/* Example Name: GridLayout Application Example * source file name: Testgridlayout * to  point: GridLayout layout Manager's Nature and usage */public class Testgridlayout{public static void Main (String args[]) {        frame f = new Frame ("Java form");        Button B1 = New button ("B1");        Button B2 = New button ("B2");        Button B3 = New button ("B3");        button B4 = New button ("B4");        button B5 = New button ("B5");        button B6 = New button ("B6");        F.setlayout (New GridLayout (3,2));        F.add (B1);                F.add (B2);        F.add (b3);                F.add (b4);        F.add (B5);                F.add (B6);        Adaptive size        f.pack ();                  F.setvisible (True);}}

Operation Result:

Five Layout manager Summary (1) Comprehensive case

Use container nesting to implement the following layout:


Testbuttons.java Source code:

import    java.awt.*;p ublic class testbuttons{public static void Main (String args[]) {frame f = new Frame ("Java form");     F.setlayout (New GridLayout (2,1));    F.setlocation (300,400);    F.setsize (400,300);    F.setbackground (New Color (204,204,255));    panel p1 = new Panel (new BorderLayout ());    panel P2 = new Panel (new BorderLayout ());    Panel p11 = new Panel (new GridLayout (2,1));    Panel p21 = new Panel (new GridLayout (2,2));    P1.add (New button ("button"), borderlayout.west);    P1.add (New button ("button"), Borderlayout.east);    P11.add (New button ("button"));    P11.add (New button ("button"));    P1.add (P11,borderlayout.center);    P2.add (New button ("button"), borderlayout.west);    P2.add (New button ("button"), Borderlayout.east);     for (int i =1;i<=4;i++) {P21.add (New button ("button"));}    P2.add (P21,borderlayout.center);    F.add (p1); F.add (p2);  F.setvisible (TRUE); }}

Run the result as above.

(2) Layout Manager Summary

The frame class is a top-level window, and the default layout manager for the frame class is borderlayout.

The Panel object cannot be displayed separately and must be added to a container.

The default layout manager for the Panel class is FlowLayout.

When a panel object is added to a container as a component, the Panel object can still have its own layout manager.

When you use the Layout manager, the layout manager is responsible for the size and position of each component, so that users cannot set the component size and location in this case

If the view is setlocation (), SetSize (), setbounds (), etc. provided by the Java language, it will be overwritten by the layout manager.

If the user does need to set the component size or location himself, the layout manager should be canceled by: setlayout (null);.



Javase learning the layout manager of 55:gui programming

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.