BorderLayout

In the BorderLayout type, the container is divided into five sections, namely, north, west, center, east, and south. When adding a component to a BorderLayout container, one of
the above five sections is specified. BorderLayout is the default layout for content panes.Figure is an example of a BorderLayout on a frame consisting of five components
(again actually buttons).



BorderLayout is an AWT feature, so to abbreviate class names, programs must include the statement

import java.awt.BorderLayout;

To create a BorderLayout container, use the following statements:

cp = this.getContentPane();
cp.setLayout(new BorderLayout());


where cp is the current applet or frame content pane. This creates no gaps between the components. If gaps are desired, use the constructor BorderLayout(horizgap, vertgap), the gaps being specified in pixels. A component is added to a BorderLayoutcontainerwith a statement of the form add(component, BorderLayout.SECTION). The following code illustrates this for Figure

cp = this.getContentPane();
cp.setLayout(new BorderLayout());
/* set up buttons and add them to content pane */
button1 = new JButton(''component 1");
cp.add(button1, BorderLayout.NORTH);
button2 = new JButton("component 2");
cp.add(button2, BorderLayout.WEST);
button3 = new JButton(''component 3"); cp.add(button3, BorderLayout.CENTER); button4 = new JButton("component 4"); cp.add(button4, BorderLayout.EAST); button5 = new JButton("component 5"); cp.add(button5, BorderLayout.SOUTH

No comments:

Post a Comment