Apr 17, 2012

Grid Layout


The grid layout manager organizes the display into a rectangular grid. It places the components into left to right and top to bottom. The grid layout class has the following constructors:
               GridLayout(int rows, int cols)

This constructs a grid layout with the specified number of rows and columns. All the components are of equal size. This is mostly used to specify that the components are of equal size. The following program place the buttons in 3 rows and 4 columns using GridLayout.

Example:

import java.awt.*;
class GridDemo extends Frame
{
GridDemo(String s)
        {
            super(s);
            setSize(300,140);
            setLayout(new GridLayout(3,4));
            for(int i=0 ; i<=12 ; i++)
            add(new Button(“Button no”+i));
            setVisible(true);
        }
}
Class test1
{
public static void main(String[] args)
        {
             GridDemo gd=new GridDemo(“Grid layout”);
        }
}

The following program illustrate the stating point of 8 square puzzle problem.

import java.awt.*;

public class eight extends java.applet.Applet;
{
public void init()
       {
             setLayout(new GridLayout(3,3));
             setFont(new Font(“SansSerif”,Font.PLAIN,24));
             for(int i=0 ; i<3 ; i++)
            {
                  for(int j=0 ; j<3 ; j++)
                 {
                     int number=i*3+j;
                     if(number>0)
                     add(new Button(“”+number));
                 }
            }
       }
}

/*
<applet code=”eight.class” width=200 height=200>
</applet>
*/



0 comments :

Post a Comment