Border layout in Java Swing

BorderLayout manage divides the container space into five sections; center,north,south,east,and west. to place the component into spacified section we need to use the strings like  “Center,” “North,” etc., or the static String fields defined in BorderLayout: BorderLayout.CENTER, BorderLayout.NORTH, etc.Components in the north and south regions will first be allotted their preferred height (if possible) and the width of the container. Once south and north components have been assigned sizes, components in the east and west regions will attempt to occupy their preferred width and any remaining height between the north and south components. A component in the center region will occupy all remaining available space.

You can place one component into one section, if you want to put more then one component then place them into panel and panel is added to container.
Constructors
BorderLayout()
Constructs a new border layout with no gaps between components.
BorderLayout(int hgap,int vgap)
Constructs a border layout with the specified gaps between components. Here hgap is the value of horizontal gap and vgep is the value of vertical gap between components.
It defines some methods to set gaps dynamically.
getHgap() Returns the horizontal gap between components. 
setHgap(int hgap) Sets the horizontal gap between components. 
getVgap() Returns the vertical gap between components. 
setVgap(int vgap) Sets the vertical gap between components. 

import java.awt.*;
import java.applet.*;
/* 
 <applet code="BorderLayoutDemo1" width=400 height=200>
 </applet>
 */
public class BorderLayoutDemo1 extends Applet
{
    public void init()
    {    
        setLayout(new BorderLayout());
        add(new Button("North"), BorderLayout.NORTH);
        add(new Button("South"), BorderLayout.SOUTH);
        add(new Button("East"), BorderLayout.EAST);
        add(new Button("West"), BorderLayout.WEST);
        add(new Button("Center"), BorderLayout.CENTER);
    }
}

No comments:

Popular Posts