Вы находитесь на странице: 1из 8

Layoutmanager

introduction
Layout means the arrangement of components within the container. In other way, we
can say that placing the components at a particular position within the container. The
task of layouting the controls is done automatically by the Layout Manager.

Layout Manager
The layout manager automatically positions all the components within the container. If
we do not use layout manager even then the components are positioned by the
default layout manager. It is possible to layout the controls by hand, but it becomes
very difficult because of the following two reasons:
It is very tedious to handle a large number of controls within the container.
More often, the width and height information of a component is not given when we
need to arrange them.
Java provides us various layout manager to position the controls. The properties like
size, shape, and arrangement varies from one layout manager to other layout
manager.The layout manager is associated with every Container object.
The awt provides four basic layout managers: FlowLayout, GridLayout,
BorderLayout, CardLayout. To create a layout manager for a given panel, create an
instance of that layout manager and then use the setLayout() method for that panel.

FlowLayout
The FlowLayout class is the most basic of layouts. Using flow layout, components are
added to the panel one at a time, row by row. If a component doesn't fit onto a row,
it's wrapped onto the next row. The flow layout also has an alignment, which
determines the alignment of each row. By default, each row is centered. Flow layout
arranges components from left to right in rows. The rows are aligned left, right, or
centered. Following are the constructors:
FlowLayout()-creates the default layout, which centers components and leaves five
pixels of space between each component
FlowLayout(int alignment)- creates a layout in which components in each row is
aligned depending upon the value of alignment. Valid values are FlowLayout.LEFT ,
FlowLayout.CENTER , FlowLayout.RIGHT
FlowLayout(int alignment, int hgap, int vgap);- creates a layout in which
components in each row is aligned depending upon the value of alignment and

Layoutmanager
horizontal and vertical space left between components is specified by hgap and vgap
respectively.
To create a basic flow layout with a centered alignment, use the following line of
code :
setLayout(new FlowLayout());
//program
import java.awt.*;
public class Flow extends Applet {
public void init() {
// Default for Applet is FlowLayout
add( new Button("One") );
add( new Button("Two") );
add( new Button("Three") );
add( new Button("Four") );
add( new Button("Five") );
}
}

Grid Layouts
Grid layouts offer more control over the placement of components inside a panel.
Using a grid layout, it is possible to split the display area of the panel into rows and
columns. Each component then added to the panel is placed in a cell of the grid,
starting from the top row and progressing through each row from left to right.
Following are the Constructors:
GridLayout()-creates a single-column grid layout.
GridLayout(int rows, int columns)-creates a grid layout with the specified number
of rows and columns GridLayout(int rows, int columns, int hor_gap, int
ver_gap)-creates a row*col grid layout with the specified horizontal and vertical gaps.
import java.awt.*;
public class Grid extends Applet {
public void init() {
setLayout( new GridLayout( 3, 2 ));
add( new Button("One") );
add( new Button("Two") );
add( new Button("Three") );
add( new Button("Four") );
add( new Button("Five") );
}

Layoutmanager
}

Border Layouts
Border layouts behave differently from flow and grid layouts. When you add a
component to a panel that uses a border layout, you indicate its placement as a
geographic direction: north, south, east, west, or center. You can also place a control
in the center of the Container. The centered control is then expanded to fill the
remaining space .It is the default LayoutManager for Windows and Frames.
Following are the constructors:
BorderLayout( ) creates a default border layout
BorderLayout(int horz, int vert) specify the horizontal and vertical space left
between components in horz and vert, respectively To use a border layout, you
create it as you do the other layouts; then you add the individual components with a
special add() method that has two arguments. The first argument is a string indicating
the position of the component within the layout, and the second is the component to
add:
Eg: add("North", new TextField("Title", 50));
import java.awt.*;
public class Border extends Applet {
public void init() {
setLayout( new java.awt.BorderLayout() );
Button n= new Button("North");
Button e=new Button("East");
Button s=new Button("South);
Button w=new Button("West");
Button c=new Button("Center");
add(n, "North" );
add(e,"East" );
add(s, "South");
add(w,"West" );
add(c,"Center" );
}
}

Layoutmanager
Card Layout Card layout behave much differently from the other layouts. When you add
components to one of the other layout managers, all those components appear on the
screen at once. Card layouts are used to produce slide shows of components, one at a
time. When you create a card layout, the components you add to the outer panel will be
other container components-usually other panels. You can then use different layouts for
those individual cards so that each screen has its own look. Cards, in a card layout, are
different panels added one at a time and displayed one at a time.
Following are the constructors:
CardLayout( ) creates a default card layout
CardLayout(int horz, int vert) allows to specify the horizontal and vertical space left
between components in horz and vert, respectively.

Handling Mouse Events

To handle mouse events, you must implement the MouseListener and the
MouseMotionListener interfaces.
import java.awt.*;

import java.awt.event.*;
import java.applet.*;
/*
<applet code="Mouse" width=500 height=500>
</applet>
*/
public class Mouse extends Applet
implements MouseListener,MouseMotionListener
{
int X=0,Y=20;
String msg="MouseEvents";
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
setBackground(Color.black);
setForeground(Color.red);
}
public void mouseEntered(MouseEvent m)
{
setBackground(Color.magenta);

msg = "Mouse entered.";


repaint();
}
public void mouseExited(MouseEvent m)
{
setBackground(Color.black);
msg="Mouse Exited";
repaint();
}
public void mousePressed(MouseEvent m)
{
X=10;
Y=20;
msg="mouse pressed";
setBackground(Color.green);
repaint();
}

Layoutmanager
public void mouseReleased(MouseEvent m)
{
X=10;
Y=20;
msg="mouse released";
setBackground(Color.blue);
repaint();
}
public void mouseMoved(MouseEvent m)
{
X=m.getX();
Y=m.getY();
msg=mouse moved;
setBackground(Color.white);
repaint();
}
public void mouseDragged(MouseEvent m)
{
setBackground(Color.yellow);
msg="Mouse Moved"+ m.getX()+" "+m.getY());
repaint();
}
public void mouseClicked(MouseEvent m)
{

msg = "Mouse clicked.";


setBackground(Color.pink);

repaint();
}
public void paint(Graphics g)
{
g.drawString(msg,X,Y);
}

Handling keyboard events


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Key" width=300 height=100>
</applet>
*/
public class Key extends Applet implements KeyListener
{
String msg = "";
int X = 10, Y = 20;
public void init()
{
addKeyListener(this);
}
public void keyPressed(KeyEvent ke)
{

Layoutmanager
showStatus("Key Down");
}
public void keyReleased(KeyEvent ke)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent ke)
{
msg += ke.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg, X, Y);
}
}

Calculator program
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code="Cal" width=300 height=300>
</applet>
*/
public class Cal extends Applet
implements ActionListener
{
String msg=" ";
int v1,v2,result;
TextField t1;
Button b[]=new Button[10];
Button add,sub,mul,div,clear,mod,EQ;
char OP;
public void init()
{
Color k=new Color(120,89,90);
setBackground(k);
t1=new TextField(10);
GridLayout gl=new GridLayout(4,5);
setLayout(gl);
for(int i=0;i<10;i++)
{
b[i]=new Button(""+i);
}
add=new Button("add");
sub=new Button("sub");
mul=new Button("mul");
div=new Button("div");
mod=new Button("mod");
clear=new Button("clear");

Layoutmanager
EQ=new Button("EQ");
t1.addActionListener(this);
add(t1);
for(int i=0;i<10;i++)
{
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
char ch=str.charAt(0);
if ( Character.isDigit(ch))
t1.setText(t1.getText()+str);
else
if(str.equals("add"))
{
v1=Integer.parseInt(t1.getText());
OP='+';
t1.setText("");
}
else if(str.equals("sub"))
{
v1=Integer.parseInt(t1.getText());
OP='-';
t1.setText("");
}
else if(str.equals("mul"))
{
v1=Integer.parseInt(t1.getText());
OP='*';
t1.setText("");
}
else if(str.equals("div"))
{
v1=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("mod"))
{
v1=Integer.parseInt(t1.getText());
OP='%';
t1.setText("");

Layoutmanager

}
if(str.equals("EQ"))
{
v2=Integer.parseInt(t1.getText());
if(OP=='+')
result=v1+v2;
else if(OP=='-')
result=v1-v2;
else if(OP=='*')
result=v1*v2;
else if(OP=='/')
result=v1/v2;
else if(OP=='%')
result=v1%v2;
t1.setText(""+result);
}
if(str.equals("clear"))
{
t1.setText("");
}

Вам также может понравиться