Java Reference
In-Depth Information
because the rows parameter is nonzero. The actual number of columns is calculated by the
layout manager.
What would happen if the setLayout statement (line 10) were replaced with
setLayout(new GridLayout(4, 2)) or with setLayout(new GridLayout(2, 2)) ?
Please try it yourself.
Note
In FlowLayout and GridLayout , the order in which the components are added
to the container is important. The order determines the location of the components
in the container.
12.5.3 BorderLayout
The BorderLayout manager divides a container into five areas: East, South, West, North,
and Center. Components are added to a BorderLayout by using add(Component,
index) , where index is a constant BorderLayout.EAST , BorderLayout.SOUTH ,
BorderLayout.WEST , BorderLayout.NORTH , or BorderLayout.CENTER . The class dia-
gram for BorderLayout is shown in Figure 12.8.
The get and set methods for these data
fields are provided in the class, but
omitted in the UML diagram for brevity.
java.awt.BorderLayout
-hgap: int
-vgap: int
The horizontal gap between the components (default: 0).
The vertical gap between the components (default: 0).
+BorderLayout()
+BorderLayout(hgap: int, vgap: int)
Creates a default BorderLayout manager.
Creates a BorderLayout manager with a specified number of
horizontal gap, and vertical gap.
F IGURE 12.8
BorderLayout lays out components in five areas.
The components are laid out according to their preferred sizes and their placement in
the container. The North and South components can stretch horizontally; the East and West
components can stretch vertically; the Center component can stretch both horizontally and
vertically to fill any empty space.
Listing 12.5 gives a program that demonstrates border layout. The program adds five
buttons labeled East , South , West , North , and Center to the frame with a BorderLayout
manager, as shown in Figure 12.9.
F IGURE 12.9
BorderLayout divides the container into five areas, each of which can hold a
component.
L ISTING 12.5 ShowBorderLayout.java
1
import javax.swing.JButton;
2
import javax.swing.JFrame;
3
import java.awt.BorderLayout;
 
Search WWH ::




Custom Search