Java graphical interface Design--GridLayout of layout manager (Grid layout)

Source: Internet
Author: User

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

Int

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

Int

Gethgap (): Gets the horizontal spacing between components.

Int

GetRows (): Gets the number of rows in this layout.

Int

Getvgap (): Gets the vertical spacing between components.

void

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

void

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

void

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

void

setrows (int rows): Sets the number of rows in this layout to the specified value.

void

Setvgap (int vgap): Sets the vertical spacing between components to the specified value.

String

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

Example one:

Gridlayoutdemo.java

Import javax.swing.*;

Import java.awt.*;

public class Gridlayoutdemo extends JFrame {

Public Gridlayoutdemo () {

SetLayout (New GridLayout (0,2)); Set to Grid layout, no number of rows specified

SetFont (New Font ("Helvetica", Font.plain, 14));

Getcontentpane (). Add (New JButton ("button 1"));

Getcontentpane (). Add (New JButton ("button 2"));

Getcontentpane (). Add (New JButton ("button 3"));

Getcontentpane (). Add (New JButton ("button 4"));

Getcontentpane (). Add (New JButton ("button 5"));

}

public static void Main (String args[]) {

Gridlayoutdemo f = new Gridlayoutdemo ();

F.settitle ("Gridwindow application");

F.pack ();

F.setvisible (TRUE);

F.setdefaultcloseoperation (Jframe.exit_on_close);

F.setlocationrelativeto (NULL); To center the form on the display

}

}

The program results are as follows:

Example two: Layout a simple calculator

Idea: Apply a border layout to a form, place a text box on North, place a panel on the center, and place a corresponding button on the calculator on the panel

Gridframe.java

Import java.awt.*;

Import javax.swing.*;

Class Gridframe extends jframe{

Define panel and set to Grid layout, 4 rows 4 columns, component horizontal, vertical spacing is 3

JPanel p=new JPanel (New GridLayout (4,4,3,3));

JTextArea t=new JTextArea (); Define a text box

Defines an array of strings, assigning values to the display text of a button

Note the order of the character elements is consistent with the Loop Add button

String str[]={"7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"};

Public Gridframe (String s) {

Super (s); Assigning a value to a form name

SetLayout (New BorderLayout ()); Defining a form layout as a boundary layout

JButton btn[]; Declaring an array of buttons

Btn=new Jbutton[str.length]; Create an array of buttons

Loop define button and add to panel

for (int i=0;i<str.length;i++) {

Btn[i]=new JButton (Str[i]);

P.add (Btn[i]);

}

Place the text box in the north position of the form

Getcontentpane (). Add (T,borderlayout.north);

Place the panel in the form center location

Getcontentpane (). Add (P,borderlayout.center);

SetVisible (TRUE);

SetSize (250,200);

Setdefaultcloseoperation (Jframe.exit_on_close);

Setlocationrelativeto (NULL); To center the form on the display

}

public static void Main (string[] args) {

Gridframe gl=new gridframe ("Grid layout computer! ");

}

}

Program execution results such as:

Note: It is important to realize that the method of adding a button through a string array and looping is used in the future, and the advantage of this method is that if you need to modify the order of the buttons, you can modify the string directly. Without having to change the code that adds the button.

Java graphical interface Design--GridLayout of layout manager (Grid 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.