Java Swing layout Management GridLayout layout

Source: Internet
Author: User

GridLayoutClass is a layout processor that lays out the components of a container in a rectangular grid. Containers are divided into equal-sized rectangles, and a component is placed in a rectangle

GridLayout Grid Layout Features: Container space is divided into a grid area of MXN columns, where only one component can be placed per region. Causes each component in the container to be a grid-like distribution of M-row xn columns.

The grid has the same width for each column and is equal to the width of the container divided by the number of columns in the grid. The grid has the same height per row and is equal to the height of the container divided by the number of rows in the grid. The components are arranged from top to bottom, from left to right.

1. Constructors

GridLayout (): Creates a grid layout with default values, that is, each component occupies one row.

GridLayout (int rows, int cols): Creates a grid layout with the specified number of rows and columns. Rows is the number of lines, cols is the number of columns.

GridLayout (int rows, int cols, int hgap, int vgap): Creates a grid layout with the specified number of rows, number of columns, and horizontal, vertical spacing of the components.

2. Methods

GetColumns (): Gets the number of columns in this layout.

Gethgap (): Gets the horizontal spacing between Components Getvgap (): Gets the vertical spacing between components.

Removelayoutcomponent (Component comp): Removes the specified component from the layout.

SetColumns (int cols): Sets the number of columns in this layout to the specified value.

Sethgap (int hgap): Sets the horizontal spacing between components to the specified value

setrows (int rows): Sets the number of rows in this layout to the specified value. Setvgap (int vgap): Sets the vertical spacing between components to the specified value.

ToString (): Returns the string representation of the value of this grid layout.

3. Example

<span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" >import Java.awt.*;import Java.applet.applet;public class Gridlayoutdemo extends applet {public void init () { SetLayout (New GridLayout (3, 2)), Add (New button ("1")), Add (New button ("2")), Add (New button ("3")), Add (New button ("4")); Add (New button ("5")), Add (New button ("6"));}} </span>
4. Results


Java Swing layout Management GridLayout layout

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.