BorderLayout
The BorderLayout manager divides the window into five
areas: East, South, West, North, and Center. Components are added to a
BorderLayout by using add(String, Component). You can use one of the following
two constructors to create a new BorderLayout.
public
BorderLayout(int hGap, int vGap)
This constructs a new BorderLayout with the specified
horizontal and vertical gaps between the components.
public
BorderLayout()
This constructs a new BorderLayout without horizontal
or vertical gaps.
Example:
import java.awt.*;
import java.awt.event.*;
class ScrollMarquee extends Canvas
{
private String message;
private int x = 10;
private int y = 10;
ScrollMarquee(String s)
{
message = s;
repaint();
}
public void left()
{
if(x>10) x = x - 10;
repaint();
}
public void right()
{
if(x<250) x = x + 10;
repaint();
}
public void paint(Graphics g)
{
g.drawString(message, x, y);
}
}
public class BorderLayoutExample extends Frame implements ActionListener
{
private Button btnLeft, btnRight, btnExit;
private ScrollMarquee cObj;
BorderLayoutExample()
{
btnLeft = new Button("Left");
btnRight = new Button("Right");
btnExit = new Button("Exit");
cObj = new ScrollMarquee("My Canvas");
cObj.setBackground(Color.yellow);
cObj.setForeground(Color.black);
Panel p = new Panel();
p.setLayout(new FlowLayout());
p.add(btnLeft);
p.add(btnExit);
setLayout(new BorderLayout());
add("Center",cObj);
add("South", p);
btnLeft.addActionListener(this);
btnRight.addActionListener(this);
btnExit.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource() == btnLeft)
{
cObj.left();
}
else if(ae.getSource() == btnRight)
{
cObj.right();
}
else if(ae.getSource() == btnExit)
{
dispose();
System.exit(0);
}
}
public static void main(String[] args)
{
BorderLayoutExample f = new BorderLayoutExample();
f.setVisible(true);
f.setSize(300,200);
}
}