Thursday 14 May 2015

BorderLayout in Java

The BorderLayout divides the container into five pieces; four form the four borders of the container and the fifth is the center. You can add one component to each of these five areas. Because the component can be a panel, you can add more than one interface element, such as a button, to each of the five areas. BorderLayout makes room for the items in the four border areas (referred to as North, South, East, and West), and then whatever is left over is assigned to the Center area. This layout is nice if you want to place scrollbars around a panel, place the scrollbars in the border regions, use all four scrollbars or just two, and place the panel you want to scroll in the center.

Listing 17.30 shows an example of the BorderLayout in action, which generates the screen

Listing 17.30. Using BorderLayout.

import java.awt.*;

import java.applet.Applet;


public class borderlayout extends Applet{
    Button the_buttons[];

    public void init()
    {   int i;
        int n_buttons; 
        BorderLayout bl; 
        String name;

        bl = new BorderLayout(10,15); 
        setLayout(bl); 
        n_buttons = 5; 
        the_buttons = new Button[n_buttons];
        //add the buttons to the various "geographic" regions
        the_buttons[0] = new Button("North Button");
        add("North",the_buttons[0]); 

        the_buttons[1] = new Button("South Button");
        add("South",the_buttons[1]); 

        the_buttons[2] = new Button("East Button");
        add("East",the_buttons[2]); 

        the_buttons[3] = new Button("West Button");
        add("West",the_buttons[3]); 

        the_buttons[4] = new Button("In the Middle");
        add("Center",the_buttons[4]); 
    }

}

No comments:

Post a Comment