Java Reference
In-Depth Information
Layout Manager
Description
Border Layout
Organizes components into five areas (North, South, East, West and
Center).
Box Layout
Organizes components into a single row or column.
Card Layout
Organizes components into one area such that only one is visible at any time.
Flow Layout
Organizes components from left to right, starting new rows as necessary.
Grid Layout
Organizes components into a grid of rows and columns.
GridBag Layout
Organizes components into a grid of cells, allowing components to span
more than one cell.
FIGURE 7.6 Some predefined Java layout managers
code sets the layout manager of a JPanel , which has a flow layout by default, so
that it uses a border layout instead:
JPanel panel = new JPanel();
panel.setLayout ( new BorderLayout());
Let's explore some of these layout managers in more
detail. We'll focus on the most popular layout managers at
this point: flow, border, box, and grid. The class presented in
Listing 7.18 contains the main method of an application that
demonstrates the use and effects of these layout managers.
The LayoutDemo program introduces the use of a
KEY CONCEPT
The layout manager for each con-
tainer can be explicitly set.
tabbed pane, a container that
allows the user to select (by clicking on a tab) which of several panes is currently
visible. A tabbed pane is defined by the JTabbedPane class. The addTab method
creates a tab, specifying the name that appears on the tab and the component to
be displayed on that pane when it achieves focus by being “brought to the front”
and made visible to the user.
Interestingly, there is an overlap in the functionality provided by tabbed panes
and the card layout manager. Similar to the tabbed pane, a card layout allows
several layers to be defined, and only one of those layers is displayed at any given
point. However, a container managed by a card layout can be adjusted only under
program control, whereas tabbed panes allow the user to indicate directly which
tab should be displayed.
In this example, each tab of the tabbed pane contains a panel that is con-
trolled by a different layout manager. The first tab simply contains a panel with
an introductory message, as shown in Listing 7.19. As we explore each layout
manager in more detail, we examine the class that defines the corresponding
panel of this program and discuss its visual effect.
 
Search WWH ::




Custom Search