Layout Management

Figure 110 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.

Figure 110. Five examples of layout management.

graphics/10fig16.gif

The Java platform supplies five commonly used layout managers: BorderLayout, BoxLayout, FlowLayout, GridBagLayout, and GridLayout.

Using Layout Managers

By default, every container has a layout manager. All JPanel objects use a FlowLayout by default, whereas content panes (the main containers in JApplet, JDialog, and JFrame objects) use BorderLayout by default. As a rule, the only time you have to think about layout managers is when you create a JPanel 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's setLayout method. For example, here's the code that makes a panel use BorderLayout:

JPanel pane = new JPanel(); 
pane.setLayout(new BorderLayout()); 

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.

Here's a quick summary of the various layout managers and where to find about them.

BorderLayout

BorderLayout is the default layout manager for every content pane. The content pane is the main container in all frames, applets, and dialogs. A BorderLayout has five areas available to hold components: north, south, east, west, and center. All extra space is placed in the center area.

graphics/10icon03.gif

The Java Tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/layout/border.html

API documentation:

http://java.sun.com/j2se/1.3/docs/api/java/awt/BorderLayout.html

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.

graphics/10icon04.gif

The Java Tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/layout/box.html

API documentation:

http://java.sun.com/j2se/1.3/docs/api/javax/swing/BoxLayout.html

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.

graphics/10icon05.gif

The Java Tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/layout/flow.html

API documentation:

http://java.sun.com/j2se/1.3/docs/api/java/awt/FlowLayout.html

GridLayout

GridLayout simply makes a bunch of components equal in size and displays them in the requested number of rows and columns.

graphics/10icon06.gif

The Java Tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/layout/grid.html

API documentation:

http://java.sun.com/j2se/1.3/docs/api/java/awt/GridLayout.html

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.

graphics/10icon07.gif

The Java Tutorial:

http://java.sun.com/docs/books/tutorial/uiswing/layout/gridbag.html

API documentation:

http://java.sun.com/j2se/1.3/docs/api/java/awt/GridBagLayout.html

Getting Started

Object-Oriented Programming Concepts

Language Basics

Object Basics and Simple Data Objects

Classes and Inheritance

Interfaces and Packages

Handling Errors Using Exceptions

Threads: Doing Two or More Tasks at Once

I/O: Reading and Writing

User Interfaces That Swing

Appendix A. Common Problems and Their Solutions

Appendix B. Internet-Ready Applets

Appendix C. Collections

Appendix D. Deprecated Thread Methods

Appendix E. Reference



The Java Tutorial(c) A Short Course on the Basics
The Java Tutorial: A Short Course on the Basics, 4th Edition
ISBN: 0321334205
EAN: 2147483647
Year: 2002
Pages: 125

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net