Laying Out Components within a Container |
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 AWT 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 the layout managers the AWT provides, gives you some general rules for using layout managers, and then tells you how to use each of the AWT layout managers. In these pages are applets illustrating layout management.
General Rules for Using Layout Managers
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?
How to Use BorderLayout
BorderLayout
is the default layout manager for allWindow
objects, such asJFrame
s andJOptionPane
s. It uses five areas 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.
Your browser can't run 1.0 Java applets, so here's a picture of the window the program brings up:
Note: Because the preceding applet runs using Java Plug-in 1.1.1, it is a Swing 1.0.3 version of the applet. To run the Swing 1.1 Beta 3 version of the applet, you can use the JDK Applet Viewer to viewBorder.html
, specifyingswing.jar
in the Applet Viewer's class path. For more information about running applets, refer to About Our Examples.How to Use CardLayout
Use theCardLayout
class when you have an area that can contain different components at different times. ACardLayout
is often controlled by aJComboBox
, with the state of theJComboBox
determining whichJPanel
(group of components) theCardLayout
displays. Here's an applet that uses aJComboBox
andCardLayout
in this way.
Your browser can't run 1.0 Java applets, so here are pictures of the window the program brings up:
Note: Because the preceding applet runs using Java Plug-in 1.1.1, it is a Swing 1.0.3 version of the applet. To run the Swing 1.1 Beta 3 version of the applet, you can use the JDK Applet Viewer to viewCard.html
, specifyingswing.jar
in the Applet Viewer's class path. For more information about running applets, refer to About Our Examples.How to Use FlowLayout
FlowLayout
is the default layout manager for eventJPanel
. 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
.
Your browser can't run 1.0 Java applets, so here's a picture of the window the program brings up:
Note: Because the preceding applet runs using Java Plug-in 1.1.1, it is a Swing 1.0.3 version of the applet. To run the Swing 1.1 Beta 3 version of the applet, you can use the JDK Applet Viewer to viewFlow.html
, specifyingswing.jar
in the Applet Viewer's class path. For more information about running applets, refer to About Our Examples.How to Use GridLayout
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.
Your browser can't run 1.0 Java applets, so here's a picture of the window the program brings up:
Note: Because the preceding applet runs using Java Plug-in 1.1.1, it is a Swing 1.0.3 version of the applet. To run the Swing 1.1 Beta 3 version of the applet, you can use the JDK Applet Viewer to viewGrid.html
, specifyingswing.jar
in the Applet Viewer's class path. For more information about running applets, refer to About Our Examples.How to Use GridBagLayout
GridBagLayout
is the most sophisticated, flexible layout manager the AWT 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 ten buttons in a panel.
Your browser can't run 1.0 Java applets, so here's a picture of the window the program brings up:
Note: Because the preceding applet runs using Java Plug-in 1.1.1, it is a Swing 1.0.3 version of the applet. To run the Swing 1.1 Beta 3 version of the applet, you can use the JDK Applet Viewer to viewGridBag.html
, specifyingswing.jar
in the Applet Viewer's class path. For more information about running applets, refer to About Our Examples.How to Use BoxLayout
Note: The box layout page is currently in the lesson about using the Swing components. Eventually, it will be moved here.]
Laying Out Components within a Container |