Tuesday, 11 March 2014

LAYOUT MANAGERS:GridLayout

A GridLayout places components in a grid of cells


GridLayout

GridLayout lays out components in a two dimensional grid.When you instantiate a gridLayout ,you define the number of rows and columns.A GridLayout places components in a grid of cells.Each component takes all the available space within its cell.Each cell is exactly the same size.Components are added to the layout from left to right, top to bottom.

The constructers supported by the GridLayout are shown here


  • GridLayout():create a single_column grid layout
  • GridLayout(int rows, int columns):Creates a grid layout with the specified number of rows and columns.
  •  GridLayout(int rows, int columns, int horizontalGap, int verticalGap):To specify the horizontal and vertical space left between components in horzontalGap and verticalGap respectively.

sample program to demonstrate GridLayout


import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class GridLayoutTest 
{
 public static void main(String[] args)
 {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("GridLayout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(3, 2));
frame.add(new JButton("Button 1"));
frame.add(new JButton("Button 2"));
frame.add(new JButton("Button 3"));
frame.add(new JButton("Button 4"));
frame.add(new JButton("Button 5"));
frame.add(new JButton("Button 6"));
frame.add(new JButton("Button 7"));
frame.add(new JButton("Button 8"));
frame.pack();
frame.setVisible(true);
 }
}

output




No comments:

Post a Comment