The BorderLayout manager is one that divides a container into five regions distinct : north ( upper region ) , south ( lower region ) , west ( left region ) , east ( right region ) and center ( central region ) . Unlike managers seen previously, the order in which the components are added is irrelevant , because at the time we add the component , in which we define the region it will stay. In each region, managed to put only one component , or is, only five (5) components may be included in this layout. if an component is inserted in a region that already contains another , this will be overlapped. Likewise the grid layout, the components are resized according to the dimensions of the container.
It may be a disadvantage we can add only 5 components, but if you add a panel in each region , we have 5 panels that have their own components and their layout managers .
For defining the BorderLayout, use the following syntax: -name container.setLayout ( new BorderLayout ( horizontal – spacing , vertical – spacing ) spacing – horizontal and vertical – spacing – > optional parameters that define the space between objects .
Distribution Manager BorderLayout divides the container JPanel into five zones, one for each component, as can be seen in Figure.
import javax.swing.*;
import java.awt.*;
public class BorderLayoutJavaExample extends JFrame
{
private JButton NrthBtn = new JButton("North Button");
private JButton SthBtn = new JButton("South Button");
private JButton EstBtn = new JButton("East Button");
private JButton WstBtn = new JButton("West Button");
private JButton CntrBtn = new JButton("Center Button");
public BorderLayoutJavaExample()
{
setLayout(new BorderLayout());
add(NrthBtn,BorderLayout.NORTH);
add(SthBtn, BorderLayout.SOUTH);
add(EstBtn, BorderLayout.EAST);
add(WstBtn, BorderLayout.WEST);
add(CntrBtn, BorderLayout.CENTER);
setVisible(true);
setSize(400, 150);
}
public static void main(String[] args)
{
BorderLayoutJavaExample frame = new BorderLayoutJavaExample();
}
}