Java Reference
In-Depth Information
Figure 12.6
Output of the BorderLayoutDemo program.
Notice that the BorderLayoutDemo takes a more object-oriented approach
to creating a Frame. The BorderLayoutDemo class extends Frame and uses
fields for each of the components in the Frame. I will try to use this
object-oriented design with the examples from here on.
The north and south buttons have their preferred height, but are as wide as
the window. The east and west buttons have their preferred width, but run the
height of the window. The center button fills in the remaining space, getting
neither its preferred width nor height.
Your initial reaction to BorderLayout might be the following: Why would
you use it? First, it can contain only five components, which seems like a seri-
ous drawback. And the way it resizes your components can make their
appearance seem irregular, as with the buttons shown in Figure 12.6. Well, it
has been my experience that BorderLayout can be quite useful, especially with
top-level containers such as Frame and JFrame (which have BorderLayout by
default). You will probably never add a Button to one of the five regions in Bor-
derLayout because of the way BorderLayout resizes buttons. Instead, you will
probably add either a component that looks appropriate in the region in which
it is placed, or you will nest a panel inside one of the regions, which is demon-
strated in the next section, Panels . I will show you how BorderLayout can be
used along with FlowLayout to create just about any GUI layout you need to
create.
Panels
A panel is a simple container used for holding components. A panel is like an
invisible container, and it must be placed in a top-level container, such as a
Frame or JFrame before it can be viewed. The java.awt.Panel class represents
Search WWH ::




Custom Search