Note: This article is based on the 31, Java graphical interface design-layout manager of the GridLayout (grid layout), the author in the learning process in accordance with their own understanding of the modified part of the code.
Grid Layout Features:
L make each component in the container 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 is the same height per row, equal to the height of the container divided by the number of rows in the grid.
L The components are arranged from top to bottom, from left to right.
The order in which the component is placed in the container determines its position in the container.
When the container size changes, the relative position of the component is constant and the size changes.
When you set the number of rows and columns for a grid layout, the number of rows or columns can have one zero. If rows is 0,cols to 3, the number of columns is fixed to 3, the number of rows is unlimited, and only 3 controls or containers can be placed on each row. If the cols is 0,rows to 3, the number of rows is fixed to 3, the number of columns is unlimited, and each row must have controls, if the number of components cannot be divisible by the number of rows, then the number of all the line components except the last line is: Math.ceil (number of components/rows).
Math.ceil (Double x): Returns the smallest integer value that is not less than X. For example, the number of rows is 3, the number of components is 13, then Math.ceil (13/3) = 5, that is, the first row, the second row of the number of components is 5, the remaining components on the last line.
If the number of components exceeds the number of grid settings, the layout Manager will automatically increase the number of meshes, the principle is to keep the number of rows unchanged.
Construction Method Summary
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.
Method Summary
GetColumns (): Gets the number of columns in this layout.
Gethgap (): Gets the horizontal spacing between components.
GetRows (): Gets the number of rows in this layout.
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.
Demo:GridLayoutDemo.java
Importjava.awt.BorderLayout;Importjava.awt.GridLayout;ImportJavax.swing.JButton;ImportJavax.swing.JFrame;ImportJavax.swing.JPanel;Importjavax.swing.SwingConstants;/** 2015-06-10*/ Public classGridlayoutdemoextendsjframe{JButton Resultbutton=NewJButton ("0"); JPanel Panel=NewJPanel (); PublicGridlayoutdemo () {//set to BorderLayoutSetLayout (NewBorderLayout ()); Add (Resultbutton, Borderlayout.north); Add (Panel, borderlayout.center); //GridLayoutGridLayout GridLayout =NewGridLayout (4,4,3,3); Panel.setlayout (gridLayout); //Add ButtonsString [] Buttonnames =NewString []{"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"}; for(String string:buttonnames) {Panel.add (NewJButton (string)); } //config ResultbuttonResultbutton.setsize (200, 50); Resultbutton.sethorizontalalignment (Swingconstants.right); Resultbutton.setenabled (false); //setSize (250,200);pack (); Settitle ("Gridlayoutdemo"); Setdefaultcloseoperation (Jframe.exit_on_close); Setlocationrelativeto (NULL); } Public Static voidMain (string[] args) {//TODO auto-generated Method StubGridlayoutdemo demo =NewGridlayoutdemo (); Demo.setvisible (true); }}
Run interface 1 as shown:
Figure 1 Running effect
Figure 2 Scaling effect
As you can see, when you use GridLayout, the width and height of the controls are appropriately adapted to the scale of the form.
swing-layout Manager gridlayout (Grid layout)-Getting Started