Layout management - Layout manager types - BorderLayout ,GridLayout ,FlowLayout and Cardlayout.
Layout Manager in Java
The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes of layout managers.
A layout manager is an object that controls the size and the position of the components in a container.
The layout manager is set by the setLayout() method. If no call to setLayout( ) is made, then the default layout manager is used.
Syntax :
void setLayout(LayoutManager layoutObj)
Here, layoutObj is a reference to the desired layout manager. If you wish to disable the layout
manager and position components manually, pass null for layoutObj.
Types of Layout managers :
There are following classes that represents the layout managers:
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
1. BorderLayout :
The BorderLayout is used to arrange the components in five regions: north, south, east, west and center. Each region (area) may contain one component only. It is the default layout of frame or window.
Constructors of BorderLayout class :
1.BorderLayout( ) :constructor creates a default border layout.
2.BorderLayout(int horz, int vert) :
Constructor allows you to specify the
horizontal and vertical space left between components in horz and vert, respectively.
The BorderLayout provides five constants for each region:
BorderLayout.CENTER BorderLayout.SOUTH
BorderLayout.EAST
BorderLayout.WEST
BorderLayout.NORTH
Or
public static final int NORTH
public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER
When adding components, you will use these constants with the following form of add( ), which is defined by Container:
void add(Component compObj, Object region)
Here, compObj is the component to be added, and region specifies where the component will
be added.
PROGRAM :
import java.awt.*;
import javax.swing.*;
public class Border
{
JFrame f;
Border()
{
f=new JFrame();
JButton b1=new JButton("NORTH");
JButton b2=new JButton("SOUTH");
JButton b3=new JButton("EAST");
JButton b4=new JButton("WEST");
JButton b5=new JButton("CENTER");
f.add(b1,BorderLayout.NORTH);
f.add(b2,BorderLayout.SOUTH);
f.add(b3,BorderLayout.EAST);
f.add(b4,BorderLayout.WEST);
f.add(b5,BorderLayout.CENTER);
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args)
{
new Border();
}
}
2. Grid Layout :
The GridLayout is used to arrange the components in rectangular grid. One component is displayed in each rectangle.
Constructors of GridLayout class :
1.GridLayout(): creates a grid layout with one column per component in a row.
creates a single-column grid layout.
2.GridLayout(int rows, int columns):
creates a grid layout with the given rows and columns but no gaps between the components.
creates a grid layout with
the specified number of rows and columns.
3.GridLayout(int rows, int columns, int hgap, int vgap):
creates a grid layout with the given rows and columns along with given horizontal and vertical gaps.
Constructor allows you to specify the horizontal
and vertical space left between components in horz and vert, respectively.
Either numRows or numColumns can be zero.
->Specifying numRows as zero allows for unlimited-length columns.
->Specifying numColumns as zero allows for unlimited-length rows.
Program :
import java.awt.*;
import javax.swing.*;
public class MyGridLayout
{
JFrame f;
MyGridLayout()
{
f=new JFrame();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.add(b6);
f.add(b7);
f.add(b8);
f.add(b9);
f.setLayout(new GridLayout(3,3));
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args)
{
MyGridLayout o= new MyGridLayout();
}
}
3. FlowLayout :
FlowLayout is the default layout manager.
The FlowLayout is used to arrange the components in a line, one after another (in a flow). It is the default layout of applet or panel.
Constructors of FlowLayout class :
1.FlowLayout( ) :
It creates the default layout, which centers components and leaves five pixels of
space between each component.
2.FlowLayout(int align) :
Constructor allows you to specify how each line is aligned.
Valid values for align are as follows:
FlowLayout.LEFT
FlowLayout.CENTER
FlowLayout.RIGHT
FlowLayout.LEADING
FlowLayout.TRAILING
Or
public static final int LEFT
public static final int RIGHT
public static final int CENTER
public static final int LEADING
public static final int TRAILING
These values specify left, center, right, leading edge, and trailing edge alignment, respectively.
3.FlowLayout(int align, int horz, int vert):
constructor allows you to specify the horizontal and vertical space left between components in horz and vert, respectively.
PROGRAM :
import java.awt.*;
import javax.swing.*;
public class MyFlowLayout
{
JFrame f;
MyFlowLayout()
{
f=new JFrame();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
f.add(b1);
f.add(b2);
f.add(b3);
f.add(b4);
f.add(b5);
f.setLayout(new FlowLayout(FlowLayout.RIGHT));
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args)
{
MyFlowLayout o=new MyFlowLayout();
}
}
4. CardLayout :
It arranges two or more components having the same size.
Card Layout is used, when we want to see only one component at a time.
The CardLayout class is unique among the other layout managers in that it stores several different layouts.
Constructors of CardLayout class :
1. CardLayout() :
Constructor creates a default card layout.
2. CardLayout(int horz, int vert) : Constructor allows you to specify the
horizontal and vertical space left between components in horz and vert, respectively.
Methods of CardLayout class:
1.void first(Container deck) :
causes the first card in the deck to be shown.
2.void last(Container deck):
causes the last card in the deck to be shown.
3.void next(Container deck) :
causes the next card in the deck to be shown.
4.void previous(Container deck) :
causes the previous card in the deck to be shown.
Both next( ) and previous( ) automatically cycle back to the top or bottom of the
deck, respectively.
5.void show(Container deck, String cardName) :
displays the card whose name is passed in cardName.
deck is a reference to the container (usually a panel) that holds the cards, and cardName is the name of a card.
Program :
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class Cardlayout extends JFrame implements ActionListener {
CardLayout card ;
JButton b1, b2, b3;
Container c;
Cardlayout ()
{
c = getContentPane();
card = new CardLayout(40, 30);
c.setLayout(card);
b1 = new JButton("first");
b2 = new JButton("second");
b3= new JButton("third");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.add("card1", b1);
c.add("card2", b2);
c.add("card3", b3);
}
public void actionPerformed(ActionEvent e)
{
card.next(this); // card.next(card2)
}
public static void main(String[] args)
{
Cardlayout cl = new Cardlayout();
cl.setSize(400, 400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
Comments
Post a Comment