NORTH
, SOUTH
, EAST
,
WEST
, and CENTER
. When adding a
component to a container with a border layout, use one of these
five constants, for example:import javax.swing.*;
import java.awt.*;
public class DemoBorderLayout extends JFrame{
public DemoBorderLayout(){
JPanel p1 = new JPanel();
BorderLayout b = new BorderLayout();
p1.setLayout(b);
JButton btnNorth = new JButton("North");
JButton btnWest = new JButton("West");
JButton btnEast = new JButton("East");
JButton btnSouth = new JButton("South");
JButton btnCenter = new JButton("Center");
p1.add(btnNorth, BorderLayout.NORTH);
p1.add(btnWest, BorderLayout.WEST);
p1.add(btnEast, BorderLayout.EAST);
p1.add(btnSouth, BorderLayout.SOUTH);
p1.add(btnCenter, BorderLayout.CENTER);
add(p1);
}
public static void main (String[]aa) {
JFrame frame = new DemoBorderLayout();
frame.setVisible(true);
frame.setSize(200,125);
frame.setDefaultCloseOperation(2);
frame.setLocationRelativeTo(null);
}
}
output :
If the window is enlarged, the center area gets as much of the available space as possible. The other areas expand only as much as necessary to fill all available space. Often a container uses only one or two of the areas of the
BorderLayout
object — just the center, or the center and the bottom.BorderLayout on Java
0 comments
Post a Comment