Page 733 - Introduction to Programming with Java: A Problem Solving Approach
P. 733

                need to position components along both dimensions (horizontal and vertical), you need to use one of the other layout managers. In this section, we discuss the BorderLayout manager, which does allow you to position components along both dimensions.
BorderLayout Regions
The BorderLayout manager is particularly useful for windows that need components near their edges. It’s common to put a title near the top edge of a window. It’s common to put a menu near the left edge of a window. It’s common to put buttons near the bottom edge of a window. The BorderLayout manager accommodates all those situations by splitting up its container into five regions, or compartments. Four of the regions are near the edges and one is in the center. You access the four edge regions with geographical names—north, south, east, and west. Note the regions’ positions in Figure 17.3.
Figure 17.3
call the setLayout method like this:
setLayout(new BorderLayout(<horizontal-gap>,<vertical-gap>));
The horizontal-gap argument specifies the number of pixels of blank space that separate the west, center, and east regions. Figure 17.3 illustrates this. The vertical-gap argument specifies the number of pixels of blank space that separate the north region from the other regions and the south region from the other re- gions. Once again, Figure 17.3 illustrates this. If you omit the gap arguments, the gap values are zero by default. In other words, if you call the BorderLayout constructor with no arguments, there will be no gaps between the regions.
The sizes of the five regions are determined at runtime, and they’re based on the contents of each re- gion. Thus, if the west region contains a long label, the layout manager attempts to widen the west region. Likewise, if the west region contains a short label, the layout manager attempts to narrow the west region.
If an outer region is empty, it collapses so that it does not take up any space. But what exactly happens during the collapse? Each outer region controls only one dividing line, so only one dividing line moves for each collapsed region. Figure 17.4 shows you that the west region’s dividing line is the boundary between west and center, the north region’s dividing line is the boundary between north and below, and so on. So if the north region is empty, the north dividing line moves all the way up to the top border, and the west, center, and east regions all expand upward. What happens if the east and south regions are both empty? The east region being
17.4 BorderLayout Manager 699
   horizontal gap
Apago PDF Enhancer
vertical gap
  north
      west
 center
   east
          south
BorderLayout regions
Assume that you’re inside a container class. To assign a BorderLayout manager to the container,















































































   731   732   733   734   735