Laying Out Components Within a Container |
Here's an applet that shows aBorderLayout
in action.
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.As the preceding applet shows, a
BorderLayout
has five areas: north, south, east, west, and center. If you enlarge the window, 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.The following code creates the
BorderLayout
and the components it manages. Here's the whole program. The program runs either within an applet, with the help ofAppletButton
, or as an application.Container contentPane = getContentPane(); //Use the content pane's default BorderLayout. //contentPane.setLayout(new BorderLayout()); //unnecessary contentPane.add(new JButton("Button 1 (NORTH)"), BorderLayout.NORTH); contentPane.add(new JButton("2 (CENTER)"), BorderLayout.CENTER); contentPane.add(new JButton("Button 3 (WEST)"), BorderLayout.WEST); contentPane.add(new JButton("Long-Named Button 4 (SOUTH)"), BorderLayout.SOUTH); contentPane.add(new JButton("Button 5 (EAST)"), BorderLayout.EAST);
Important: When adding a component to a container that usesBorderLayout
, specify the component's location as one of the arguments to theadd
method. Do not rely on the component being added to the center, by default. If you find that a component is missing from a container controlled by aBorderLayout
, make sure that you specified the component's location and that you didn't put another component in the same location.
All our examples that use
BorderLayout
specify the component as the first argument to theadd
method. For example:However, you might see code in other programs that specifies the component second. For example, the following are alternate ways of writing the preceding code:add(component, BorderLayout.CENTER) //we prefer this formadd(BorderLayout.CENTER, component) //valid but old-fashioned or add("Center", component) //valid but error proneBy default, a
BorderLayout
puts no gap between the components it manages. In the preceding applet, any apparent gaps are the result of theJButton
s reserving extra space around their apparent display area. You can specify gaps (in pixels) using the following constructor:public BorderLayout(int horizontalGap, int verticalGap)Examples that Use BorderLayout
The following table lists some of the many examples that useBorderLayout
.
Example Where Described Notes BorderWindow.java
This page Puts a component in each of the five possible locations. TabbedPaneDemo.java
How to Use Tabbed Panes One of many examples that puts a single component in the center of a content pane, so that the component is as large as possible. CheckBoxDemo.java
How to Use Check Boxes Creates a JPanel
that uses aBorderLayout
Puts components into the west and center locations.ComboBoxDemo.java
How to Use Combo Boxes Puts components into the north and south locations of a content pane, leaving the center empty.
Laying Out Components Within a Container |