You are on page 1of 5

Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Adv. Java Lecture-6


Topic: GridLayout, NullLayout, Panel

GridLayout:-
 A GridLayout lays out components in a grid of equal
sized rectangles.

 The illustration shows how the components would be


arranged in a grid layout with 3 rows and 2 columns.

 If a container uses a GridLayout, the appropriate add() method takes


a single parameter of type Component (for example:
add(myButton)).

 Components are added to the grid in the order shown; that is, each
row is filled from left to right before going on the next row.

 The constructor for a GridLayout with R rows and C columns takes


the form GridLayout(R,C)

 If you want to leave horizontal gaps of H pixels between columns and


vertical gaps of V pixels between rows, use GridLayout(R,C,H,V)
instead.

Constructors of Grid Layout:


GridLayout()
Creates a grid layout with a default of one column per
component, in a single row.
GridLayout(int rows, int cols)
Creates a grid layout with the specified number of rows
and columns.
GridLayout(int rows, int cols, int hgap, int vgap)
Creates a grid layout with the specified number of rows
and columns.

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Program to demonstrate GridLayout:


import java.awt.*;

class GridTest extends Frame


{
GridTest()
{
setSize(400,300);
setTitle("Grid Layout Test");
setLayout(new GridLayout(3,4));

for(int i=1;i<=12;i++)
{
Button b = new Button("Button "+i);
add(b);
}
}
public static void main(String args[])
{
GridTest f = new GridTest();
f.setVisible(true);
}
}
Output:

3 Rows

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

4 Rows
Null Layout:
 By default, the Frame has a BorderLayout manager.

 The layout manager is used to place widgets onto the containers.

 If we call setLayout(null) we can position our components absolutely.


For this, we use the setBounds() method which takes four
parameters.
setBounds(x,y,width,height);

Program to demonstrate Null Layout:


import java.awt.*;
class NullTest extends Frame
{
NullTest()
{ setSize(600,500);
setTitle("Null Test ");
setLayout(null);

Button b1 = new Button("Ok");


b1.setBounds(100,100,80,50);
Button b2 = new Button("Cancel");
b2.setBounds(190,100,80,50);
add(b1);
add(b2);
}
public static void main(String [] args)
{
NullTest f = new NullTest();
f.setVisible(true);
}
}
Output

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Panel:
 Panel is the simplest container class. A panel provides space in which an
application can add any other component, including other panels.

 The default layout manager for a panel is the FlowLayout layout


manager.

 Panel is border less container.

Program to demonstrate Null Layout:


import java.awt.*;

class PanelFrame extends Frame


{
PanelFrame()
{
setSize(600,500);
setTitle("Panel Frame");

Panel north = new Panel();


north.add(new Button("Red"));
north.add(new Button("Green"));
north.add(new Button("Blue"));
//north.setBackground(Color.red);
add(north,"North");

Panel south = new Panel();


south.add(new Button("Ok"));
south.add(new Button("Cancel"));
//south.setBackground(Color.green);
add(south,"South");
}
public static void main(String [] args)
{
PanelFrame f = new PanelFrame();
f.setVisible(true);
}
}

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260
Course: Advanced Java, Prepared By: Atul Kabra, 9422279260

Output:

Course: Advanced Java, Info Planet Programming Classes Prepared By: Atul Kabra, 9422279260

You might also like