Layout manager for JAVA GUI programming

Source: Internet
Author: User

The Java GUI (graphical user interface) consists of various components, mainly the AWT components (java.awt) and the more powerful swing components (javax.swing).

Components can be divided into container components and non-container components. A container component is a component that can contain other components, and is divided into top-level containers and general-purpose containers. Non-container components must be included in the container.

The layout of the component, which contains the location and size of each component, is usually the responsibility of the layout manager. Each container has a default layout manager that can change the container's layout manager through the container's SetLayout () method.

Let's take a look at some of the common layout managers available in Java.

1.FlowLayout

defined in the java.awt package. It is laid out by placing the component one line at a container at a single line, and a new row with a row full.

There are 3 ways of structuring:

FlowLayout (): Default, center alignment, horizontal and vertical spacing is 5 pixels.

FlowLayout (int align): align Specifies the alignment, and the horizontal and vertical spacing is 5 pixels. The desirable values of align are flowlayout.left, Flowlayout.right, Flowlayout.center.

FlowLayout (int align,int hgap,int vgap): align specifies alignment, HGAP specifies horizontal spacing, and vgap specifies vertical spacing.

Unlike other layout managers, FlowLayout does not force the size of the component, allowing the component to have a custom size. Each component has a getpreferredsize () method, and the container's layout manager calls this method to get a custom size for each component.

2.BorderLayout

defined in the java.awt package. Is the default layout manager for the Contents pane (Container contentpane, Content pane get Method Getcontentpane ()) in the top-level container (JFrame, JApplet, JDialog, JWindow).

Provides a more complex component layout management: dividing a container into 5 regions, East (Borderlayout.north) south (Borderlayout.south) west (borderlayout.west) north (Borderlayout.east) (Borderlayout.center), each region can add a component.

There are 2 ways of structuring:

BorderLayout (): By default, there is no space between components.

BorderLayout (int hgap,int vgap): Specifies the horizontal spacing (hgap), Vertical spacing (vgap).

Component additions:

The component must be joined to the specified area in the container through the Add () method. The default is placed in the center area.

If you want to display multiple components in an area, you can first place an internal container in the area, such as the JPanel component, and then place the required components into JPanel and drop them into the specified area. Complex layouts can be constructed through nesting of internal containers.

3.GridLayout

defined in the java.awt package. The layout is a grid format, dividing the container into several rows of grids multiplied by several columns, in which the components are sequentially placed, each of which occupies one grid.

There are 3 ways of structuring:

GridLayout (): Only one row of grids, the number of columns in the grid varies according to actual needs.

GridLayout (int rows,int cols): Specifies the number of rows (rows), and the number of columns (COLS).

GridLayout (int rows,int cols,int hgap, int vgap): Specify the number of rows (rows), Number of columns (cols), specify horizontal spacing (hgap), Vertical spacing (vgap).

Rows,cols can be 0 (size varies according to actual needs), but not 0 at a time. The width of each column of the grid is the same, and the height of each row of the grid is the same. The component is placed in the order of the container, filled from left to right, and then into the next line. Leave White to add a blank label.

The relationship between the number of meshes and the number of components: Add less Fill (blank label).

The size of the container changes, the relative position of the component is unchanged, and the size changes.

4.CardLayout

defined in the java.awt package. The layout is a card, and the container is treated as a series of cards, and each moment only shows one of them, and the container acts as a container for the card.

Show rules for advanced display first. The order of the cards is determined by the order of the component objects themselves inside the container.

There are 2 ways of structuring:

CardLayout (): Default, no spacing.

CardLayout (int hgap,int vgap): Specifies the horizontal spacing (HGAP), specifying the vertical spacing (vgap).

Common methods:

Add (): Container add component.

public void First (Container parent): Flips to the top of the container.
public void Next (Container parent): Flips to the next sheet of the container. In the end, turn the first one.
public void Previous (Container parent): Flips to the previous sheet of the container. The head turns to the last one.
public void last (Container parent): Flips to the final sheet of the container.
public void Show (Container parent, String name): Flips the specified name card that has been added. Does not exist then does not respond.

5.BoxLayout

defined in the Javax.swing package. Layout by: Line up the components horizontally, or in a vertical direction.

Line up each component can have a different width, and each component can have a different height in a row.

There are only 1 methods of construction:

BoxLayout (Container target,int axis): Container object parameter target specifies the container to which the layout is to be applied, the axis settings are arranged horizontally (Boxlayout.x_axis) or vertically (boxlayout.y_ AXIS).

A container--box class that specifically uses boxlayout is defined in the Javax.swing package.

To create a box static method:

public static Boxcreatehorizontalbox (): horizontal-oriented boxlayout.

public static Boxcreatevertitcalbox (): BoxLayout in the vertical direction.

Ways to create a box invisible component:

The function is to increase the distance between courseware components.

public static Componet Createhorizonalglue ().
public static Componet Createverticalglue ().
public static componet createhorizonalstrut (int width).
public static componet createverticalstrut (int height).
public static componet Createrigidarea (Dimension D).

6. Empty layout

Layout Manager is not available for special occasions.

Call the container's setlayout (null) to set the layout manager to NULL, and then call the component's setbounds (int x, int y, int width, int height) to set the component size position.

Layout manager for JAVA 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.