Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The following figure shows the GUIs of five programs, each of which displays five buttons. The buttons are identical, and the code for the programs is almost identical. So why do the GUIs look so different? Because they use different layout managers to control the size and the position of the buttons.The Java platform supplies five commonly used layout managers: BorderLayout
,BoxLayout
,FlowLayout
,GridBagLayout
, andGridLayout
.
By default, every container has a layout manager. AllJPanel
objects use aFlowLayout
by default, whereas content panes (the main containers inJApplet
,JDialog
, andJFrame
objects) useBorderLayout
by default. As a rule, the only time you have to think about layout managers is when you create aJPanel
or add components to a content pane. If you don't like the default layout manager that a panel or content pane uses, you can change it to a different one. Just invoke the container'ssetLayout
method. For example, here's the code that makes a panel useBorderLayout
:When you add components to a panel or a content pane, the arguments you specify to the add method depend on the layout manager that the panel or content pane is using. So be sure to check the API documentation for the layout manager for details.JPanel pane = new JPanel(); pane.setLayout(new BorderLayout());Here's a quick summary of the various layout managers and where to find more information in The Java Tutorial and API documentation.
BorderLayout
BorderLayout
is the default layout manager for every content pane. 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.
Tutorial: How to Use BorderLayout API documentation: BorderLayout
BoxLayout
- The BoxLayout class puts components in a single row or column. This class respects the components' requested maximum sizes and also lets you align components.
Tutorial: How to Use BoxLayout API documentation: BoxLayout
FlowLayout
- FlowLayout is the default layout manager for every JPanel. This layout manager simply lays out components from left to right, starting new rows, if necessary.
Tutorial: How to Use FlowLayout API documentation: FlowLayout
GridLayout
- GridLayout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns.
Tutorial: How to Use GridLayout API documentation: GridLayout
GridBagLayout
- GridBagLayout is the most sophisticated, flexible layout manager the Java platform provides. This layout manager 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.
Tutorial: How to Use GridBagLayout API documentation: GridBagLayout
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.