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