LAYOUT MANAGERS
Alayout manager automatically arranges your control with in a window by using same type of Algorithm.The layout manager is set by the setlayout. We have several predefined layout manager classes.
Flowlayout:
It is a default layoutmanager components are laidout from the upper left corner to right & top to bottom.
FlowLayout( )
FlowLayout(how)
FlowLayout(how,horizontal,vertical)
Where how=FlowLayout.LEFT
FlowLayout.CENTER
FowLayout.RIGHT
WRITE A PROGRAM ON LEFT ALIGNED FLOWLAYOUT.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Flow extends Applet implements ItemListener
{
String s=" ";
Checkbox Win98,WinNt,Solaris,Mac;
public void init()
{
setLayout(new FlowLayout(FlowLayout.LEFT));
Win98=new Checkbox("Windows98",true);
WinNt=new Checkbox("WindowsNt");
Solaris=new Checkbox("Solaris");
Mac=new Checkbox("Macos");
add(Win98);
add(WinNt);
add(Solaris);
add(Mac);
Win98.addItemListener(this);
WinNt.addItemListener(this);
Solaris.addItemListener(this);
Mac.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
repaint();
}
public void paint(Graphics g)
{
String s="Current State: ";
g.drawString(s,6,80);
s=" Windows98: "+Win98.getState();
g.drawString(s,6,100);
s=" WindowsNt: "+WinNt.getState();
g.drawString(s,6,120);
s=" Solaris: "+Solaris.getState();
g.drawString(s,6,140);
s=" Macos: "+Mac.getState();
g.drawString(s,6,180);
}
}
/*<applet code=Flow height=180 width=300>
</applet>*/

BorderLayout:
It has four narrow fixed widths. Components at the edges and one large area in the center the four sides are refer to as north,south,east,west.The middle Area is called center.
Constructors:
BorderLayout(int horizontal,int vertical)
Methods
void add(component object)
WRITE A PROGRAM ON BORDER LAYOUT.
import java.awt.*;
import java.applet.*;
import java.util.*;
public class Border extends Applet
{
public void init()
{
setLayout(new BorderLayout());
add(new Button("This is across the top."),BorderLayout.NORTH);
add(new Button("This footer message might go here."),
BorderLayout.SOUTH);
add(new Button("Right"),BorderLayout.EAST);
add(new Button("Left"),BorderLayout.WEST);
String s="The reasonable man adapts"+
"himself to the world;\n"+
"trying to adapt the world to himself.\n"+
"Therefore all progress depends"+
"on the unresonable man.\n\n"+
" -D.G.Prasad \n\n";
add(new TextArea(s),BorderLayout.CENTER);
}
}
/*<applet code=Border width=400 height=200>
</applet>*/

UsingInsets:
Using Insets we can leave some space between the container.
Insets(top,left,Botyom,right)
Insets getInsets( )
WRITE A PROGRAM ON INSETS.
import java.awt.*;
import java.applet.*;
import java.util.*;
public class Inset extends Applet
{
public void init()
{
setLayout(new BorderLayout());
add(new Button("This is across the top."),BorderLayout.NORTH);
add(new Button("This footer message might go here."),
BorderLayout.SOUTH);
add(new Button("Right"),BorderLayout.EAST);
add(new Button("Left"),BorderLayout.WEST);
String s="The reasonable man adapts"+
"himself to the world;\n"+
"trying to adapt the world to himself.\n"+
"Therefore all progress depends"+
"on the unresonable man.\n\n"+
" -D.G.Prasad \n\n";
add(new TextArea(s),BorderLayout.CENTER);
}
public Insets getInsets()
{
return new Insets(10,30,10,10);
}
}
/*<applet code=Inset width=400 height=200>
</applet>*/

Gridlayout:
This is used to create layout components in a two dimensional grid.
Constructors:
GridLayout( )
GridLayout(rows,columns)
GridLayout(rows,columns,hprizontal,vertical)
WRITE A PROGRAM ON GRIDLAYOUT.
import java.awt.*;
import java.applet.*;
public class Grid extends Applet
{
public void init()
{
setLayout(new GridLayout(2,2));
for(int i=1,k=1;i<=2;i++)
{
for(int j=1;j<=2;j++,k++)
{
add(new Button(" "+k));
}
}
}
}
/*<applet code=Grid width=300 height=200>
</applet>*/

CardLayout:
Card layout class is an unquie among the other layout managers in that it stores several different layouts.
GridLayout( )
GridLayout(horizontal,vertical)
The cards are typically held in an object of type panel.This panel must have a card layout selected as slaved manager. when card panels are added to apanel they are usually given a name. we will use the add method when adding cards to a pannel.
void add(component,object)
After creating of a deck, program activeated a card by calling one of the following methods.
void first(deck)
void last(deck)
void previous(deck)
void next(deck)
void show(deck,cardname)
WRITE A PROGRAM ON CARD LAY OUT.
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
public class Card extends Applet
implements ActionListener,MouseListener
{
Checkbox Win98,WinNt,Solaris,mac;
Panel osCards;
CardLayout cardLO;
Button Win,Other;
public void init()
{
Win=new Button("Windows");
Other=new Button("Other");
add(Win);
add(Other);
cardLO=new CardLayout();
osCards=new Panel();
osCards.setLayout(cardLO);
Win98=new Checkbox("Windows98",null,true);
WinNt=new Checkbox("WindowsNt");
Solaris=new Checkbox("Soalris");
mac=new Checkbox("Macos");
Panel winPan=new Panel();
winPan.add(Win98);
winPan.add(WinNt);
Panel otherPan=new Panel();
otherPan.add(Solaris);
otherPan.add(mac);
osCards.add(winPan,"Windows");
osCards.add(otherPan,"Other");
add(osCards);
Win.addActionListener(this);
Other.addActionListener(this);
addMouseListener(this);
}
public void mousePressed(MouseEvent me)
{
cardLO.next(osCards);
}
public void mouseClicked(MouseEvent me)
{
}
public void mouseEntered(MouseEvent me)
{
}
public void mouseExited(MouseEvent me)
{
}
public void mouseReleased(MouseEvent me)
{
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==Win)
{
cardLO.show(osCards,"Windows");
}
else
{
cardLO.show(osCards,"Others");
}
}
}
/*<applet code=Card width=300 height=100>
</applet>*/

