-BorderLayout()
creates a default border layout with no space between components.
-BorderLayout(int hor,int ver)
creates a border layout with spaces between components.
In the example code below, the Rotate button is placed at the north part, the Next button at the west part, the Previous button at the east part, the Zoom in and Zoom out buttons at the south part of the Frame window. The image that is displayed on the Label component is shown at the center part of the Frame window.
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.Graphics;
import javax.imageio.ImageIO;
class DrawArea extends Label{
BufferedImage bi;
DrawArea(){
try{
bi=ImageIO.read(new File("d:/earth.png")); //read image file
}catch(IOException ie){}
}
public void paint(Graphics g){
g.drawImage(bi,0,0,null);
}
}
class InterfaceShow extends Frame{
InterfaceShow(String title){
setTitle(title);
setLayout(new BorderLayout());
setSize(new Dimension(500,300));
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Panel p=new Panel();
p.setLayout(new FlowLayout());
Button rotatebt=new Button("Rotate");
Button zoominbt=new Button("Zoom in");
Button zoomoutbt=new Button("Zoom out");
Button movenextbt=new Button("Next");
Button movepreviousbt=new Button("Previous");
p.add(zoominbt);
p.add(zoomoutbt);
add(rotatebt,BorderLayout.NORTH);
add(movenextbt, BorderLayout.WEST);
add(movepreviousbt,BorderLayout.EAST);
add(new DrawArea(), BorderLayout.CENTER);
add(p, BorderLayout.SOUTH);
validate();
}
}
public class BorderLayoutTest{
public static void main(String[] args){
new InterfaceShow("Using BorderLayout");
}
}
import java.awt.Button;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.Graphics;
import javax.imageio.ImageIO;
class DrawArea extends Label{
BufferedImage bi;
DrawArea(){
try{
bi=ImageIO.read(new File("d:/earth.png")); //read image file
}catch(IOException ie){}
}
public void paint(Graphics g){
g.drawImage(bi,0,0,null);
}
}
class InterfaceShow extends Frame{
InterfaceShow(String title){
setTitle(title);
setLayout(new BorderLayout());
setSize(new Dimension(500,300));
setVisible(true);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Panel p=new Panel();
p.setLayout(new FlowLayout());
Button rotatebt=new Button("Rotate");
Button zoominbt=new Button("Zoom in");
Button zoomoutbt=new Button("Zoom out");
Button movenextbt=new Button("Next");
Button movepreviousbt=new Button("Previous");
p.add(zoominbt);
p.add(zoomoutbt);
add(rotatebt,BorderLayout.NORTH);
add(movenextbt, BorderLayout.WEST);
add(movepreviousbt,BorderLayout.EAST);
add(new DrawArea(), BorderLayout.CENTER);
add(p, BorderLayout.SOUTH);
validate();
}
}
public class BorderLayoutTest{
public static void main(String[] args){
new InterfaceShow("Using BorderLayout");
}
}
No comments:
Post a Comment