Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Every container, by default, has a layout manager -- an object that implements theLayoutManager
interface.* If a container's default layout manager doesn't suit your needs, you can easily replace it with another one. The Java platform supplies layout managers that range from the very simple (FlowLayout
andGridLayout
) to the special purpose (BorderLayout
andCardLayout
) to the very flexible (GridBagLayout
andBoxLayout
).This section gives you an overview of some layout managers that the Java platform provides, gives you some general rules for using layout managers, and then tells you how to use each of the provided layout managers. It also points to examples of using each layout manager.
This section answers some common questions about layout managers:
- How do you choose a layout manager?
- How do you create a layout manager, associate it with a container, and tell it to start working?
- How does the layout manager know what components it manages?
BorderLayout
is the default layout manager for every content pane. (As described in Using Top-Level Containers, the content pane is the main container in all frames, applets, and dialogs.) ABorderLayout
has five areas available to hold components: north, south, east, west, and center. All extra space is placed in the center area. Here's an applet that puts one button in each area:
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.
TheBoxLayout
class puts components in a single row or column. It respects the components' requested maximum sizes, and also lets you align components. Here's an applet that uses aBoxLayout
to put a bunch of buttons in a centered column:
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.
TheCardLayout
class lets you implement an area that contains different components at different times. Tabbed panes are intermediate Swing containers that provide similar functionality, but with a pre-defined GUI. ACardLayout
is often controlled by a combo box , with the state of the combo box determining which panel (group of components) theCardLayout
displays. Here's an applet that uses a combo box andCardLayout
in this way:
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.
FlowLayout
is the default layout manager for everyJPanel
. It simply lays out components from left to right, starting new rows if necessary. Both panels in theCardLayout
applet above useFlowLayout
. Here's another example of an applet that uses aFlowLayout
:
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.
GridLayout
simply makes a bunch of components equal in size and displays them in the requested number of rows and columns. Here's an applet that uses aGridLayout
to control the display of five buttons:
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.
GridBagLayout
is the most sophisticated, flexible layout manager the Java platform provides. It aligns components by placing them within a grid of cells, allowing some components to span more than one cell. The rows in the grid aren't necessarily all the same height; similarly, grid columns can have different widths. Here's an applet that uses aGridBagLayout
to manage five buttons:
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.
LayoutManager2
, was introduced.
LayoutManager2
extends
LayoutManager
,
providing support for maximum size and alignment.
Currently, only BoxLayout
implements
LayoutManager2
.
All the other layout managers that we discuss implement only
LayoutManager
.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2001 Sun Microsystems, Inc. All rights reserved.