All About Class GridLayout in AWT in Java



GridLayout provides flexibility for placing components.It causes container's component to be laid out in a rectangular grid.The container is split into equal sized rectangles.One component is placed into each rectangle.We can create the manager with a number of rows and columns.Components then fill up the calls defined by the manager.
The class structure of class GridLayout is given below:


public class java.awt.GridLayout extends java.lang.Object implements java.awt.LayoutManager{
//constructor
public GridLayout(int rows,int cols);
public GridLayout(int rows,int cols,int hgap,int vgap);
//methods
public void addLayoutComponent(String name,Component comp);
public void layoutContainer(Container target);
public Dimension minimumLayoutSize(Container target);
public Dimension preferredLayoutSize(Container target);
public void removeLayoutSize(Container target);
public String toString();
}
 
public GridLayout(int rows,int cols);
creates a grid layout with the specified number of rows and columns.All components in the layout are given equal size.One but not both of rows and columns can be zero,which means that any number of objects can be placed in a row or in a column.
public GridLayout(int rows,int cols,int hgap,int vgap);
creates a grid layout with the specified number of rows and columns.All components in the layout are given equal size.In addition the horizontal and vertical gaps are placed at the left and right edge and between each of the columns.The vertical gaps are placed at the top and bottom edge and between each rows.One but not both of rows and columns can be zero,which means that any number of objects can be placed in a row or in a column.

Example

Button b1,b2,b3,b4,b5,b6;
Frame frame=new Frame();
frame.setLayout(new GridLayout(3,2));//3 rows and 2 columns
b1=new Button("B1");
b2=new Button("B2");
b3=new Button("B3");
b4=new Button("B4");
b5=new Button("B5");
b6=new Button("B6");
frame.add(b1);frame.add(b2);frame.add(b3);
frame.add(b4);frame.add(b5);frame.add(b6);
 
All About Class GridLayout in AWT in Java All About Class GridLayout in AWT in Java Reviewed by Animesh Chatterjee on October 23, 2018 Rating: 5

No comments:

Powered by Blogger.