Showing posts with label gridlayout. Show all posts
Showing posts with label gridlayout. Show all posts

Wednesday, July 31, 2013

GridLayout

GridLayout is another layout manager. It displays components in a grid or table. A cell of the grid or table is for one component. You have three ways to create a grid layout manager by using the following constructors:

-GridLayout()
creates a grid layout that has only one column.
-GridLayout(int rows, int columns)
creates a grid layout with specified number of rows and columns.
-GridLayout(int rows,int columns,int hor,int ver)
creates a grid layout with specified number of rows and columns, and spaces between components.

In the example code below, a grid layout with five rows and two columns is created. Each cell of the grid contains a button object.

import java.awt.Button;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class InterfaceShow extends Frame{
InterfaceShow(String title){
setTitle(title);
setLayout(new GridLayout(5,2));
setSize(new Dimension(500,300));
setBackground(Color.BLACK);
setVisible(true);

addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
for(int i=0;i<10;i++){

add(new Button(i+""));
}

validate();
}
}
public class GridLayoutTest {
public static void main(String[] args){
new InterfaceShow("Using GridLayout");

}
}

gridlayout layoutmanager