Professional Documents
Culture Documents
This fluidity works in your favor, because it takes into account the differences in how interface components
are displayed on different operating systems. On the same Java program, a clickable button might look
different in Windows than it does in Linux or Mac OS. This is a departure from languages such as Microsoft
Visual Basic, where you normally specify exactly where a component will appear and what it looks like.
Components are organized in an interface by using a set of classes called layout managers. These classes
define how components will be displayed within a container—during the previous hour, you used the
FlowLayout class to arrange components. Each container in an interface can have its own layout
manager.
There are several layout managers you can use to affect how components are shown. The default
manager for panels is the FlowLayout class, which was used during the previous hour.
Under FlowLayout, components are dropped onto an area in the same way words are organized on a
printed page in English—from left to right, then on to the next line when there's no more space.
• Set up the container's layout manager by calling the Container object's setLayout() method with
the FlowLayout object as an argument.
The following example could be used in a frame so that it will employ flow layout when components are
added:
You can also set up a layout manager to work within a specific container, such as a JPanel object. You
can do this by using the setLayout() method of that container object. The following statements create a
JPanel object called inputArea and set it up to use FlowLayout as its layout manager:
To give you an idea of how the different layout managers work, a simple application will be shown under
each of the classes. The Crisis application has a graphical user interface with five buttons. Load your
word processor and open up a new file called Crisis.java. Enter Listing 14.1 and save the file when
you're done.
After compiling the Crisis application, you can run it with the following command:
java Crisis
The FlowLayout class uses the dimensions of its container as the only guideline for how to lay out
components. Resize the window of the application to see how components are instantly rearranged. Make
the window twice as wide, and you'll see all of the JButton components are now shown on the same line.
Java programs will often behave differently when their display area is resized.
columns. All components are allocated the same amount of size in the display area, so if you specify a grid
that is three columns wide and three rows tall, the container will be divided into nine areas of equal size.
The following statements create a container and set it to use a grid layout that is two columns wide and
three rows tall:
Figure 14.2 shows what the Crisis application would look like if it used grid layout.
Some of the labels in Figure 14.2 display text that has been shortened—if the text is wider than the area
available in the component, the label will be shortened using ellipses (…).
GridLayout places all components as they are added into a place on a grid. Components are added
from left to right until a row is full, and then the leftmost column of the next grid is filled.
The next layout manager left to experiment with is the BorderLayout class. The following statements
create a container that uses border layout, placing components at specific locations within the layout:
second argument to specify where the component should be placed. This argument should be one of five
class variables of the BorderLayout class: NORTH, SOUTH, EAST, WEST, and CENTER are used for this
argument.
Like the GridLayout class, BorderLayout devotes all available space to the components. The
component placed in the center is given all the space that isn't needed for the four border components, so
it's usually the largest.
As you are arranging components within a container, you might want to move components away from the
edges of the container. This is accomplished using Insets, an object that represents the border area of a
container.
The Insets class, which is part of the java.awt package, has a constructor method that takes four
arguments: the space to leave at the top, left, bottom, and right on the container. Each argument is
specified using pixels, the same unit of measure employed when defining the size of a frame.
The around object represents a container border that is 10 pixels inside the top edge, 6 pixels inside the
left, 10 pixels inside the bottom, and 3 pixels inside the right.
To make use of an Insets object in a container, you must override the container's getInsets()
method. This method has no arguments and returns an Insets object, as in the following example:
Figure 14.4 shows how this would change one of the preceding examples, the BorderLayout-managed
Note
That last measurement isn't an error, though you may think it is after looking at Figure 14.4 again. Visually,
the top and bottom border both look about the same size. There's a reason for this: A JFrame container
automatically has a top inset value of 35 to make room for the frame's title bar. When you override
getInsets() and set your own values, anything less than 35 will cause the container to display
components underneath the title bar. You must create an Insets object with a top value of at least 35 for
One way around this problem is to use a group of JPanel objects as containers to hold different parts of a
graphical user interface. You can set up different layout rules for each of these parts by using the
setLayout() methods of each JPanel. Once these panels contain all of the components they need to
contain, the panels can be added directly to the content pane.
This hour's workshop will be to develop a full interface for the program you will write during the next hour.
The program is a Lotto number cruncher that will assess a user's chance of winning one of the multimillion-
dollar Lotto contests in the span of a lifetime. This chance will be determined by running random six-
number Lotto drawings again and again until the user's numbers turn up as a big winner. Figure 14.5
shows the GUI you will be developing for the application.
Figure 14.5 Displaying the graphical user interface of the LottoMadness application.
Create a new file in your word processor called LottoMadness.java. Enter Listing 14.2 and save the
After you compile this application, run it to see how the LottoMadness application will work. Even though
you haven't added any statements that make the program do anything yet, you can make sure that the
graphical interface is organized correctly and collects the information you need.
This application uses several different layout managers. If you look carefully at each of the components,
you might be able to determine which manager is in use in the different areas of the program. To get a
clearer picture of how the application's user interface is laid out, take a look at Figure 14.6. The interface is
divided into five horizontal rows which are separated by horizontal black lines in the figure. Each of these
rows is a JPanel object, and the overall layout manager of the application organizes these rows into a
GridLayout of five rows and one column.
Within the rows, different layout managers are used to determine how the components should appear.
Rows 1 and 3 use FlowLayout objects. Lines 45–46 of the program show how these are created:
Three arguments are used with the FlowLayout() constructor method. The first argument,
FlowLayout.CENTER, indicates that the components should be centered within their container—the
horizontal Panel on which they are placed. The last two components specify the width and height that
each component should be moved away from other components. Using a width of 10 pixels and a height of
10 pixels puts a small amount of extra distance between the components.
Row 2 of the interface is laid out into a grid that is two rows tall and seven columns wide. The
GridLayout() constructor also specifies that components should be set apart
from other components by 10 pixels in each direction. Lines 54–55 set up
this grid:
Row 4 uses GridLayout to arrange components into a grid that is two rows
tall and three columns wide.
In Lines 45–100, the components are added. Lines 45–52 are indicative of
the entire LottoMadness() constructor method:
Summary
When you design a Java program's graphical user interface for the first time, you may have trouble
believing that it's an advantage for components to move around. There's an appeal to seeing something
exactly in the way you designed it—a feature that's called WYSIWYG, an acronym for "What You See Is
What You Get."
Because Java was designed to run on a wide variety of operating systems and computing environments,
interface layout was designed to be TNTWTMLL—There's No Telling What This Might Look Like.
Layout managers provide a way to develop an attractive graphical user interface that is flexible enough to
handle differences in presentation.
During the next hour, you'll learn more about the function of a graphical user interface. You'll get a chance
to see the LottoMadness interface in use as it churns through lottery drawings and tallies up winners