Java Reference
In-Depth Information
add the panel. The pack method of the frame sets its size appropriately based on
its contents—in this case the frame is sized to accommodate the size of the panel it
contains. This is a better approach than trying to set the size of the frame explic-
itly, which should change as the components within the frame change. The call to
the setVisible method causes the frame to be displayed on the monitor screen.
The Authority program is not interactive. In general, labels do not allow the
user to interact with a program. We will examine interactive GUI components in
the next chapter.
However, you can interact with the frame itself in various ways. You can move
the entire frame to another point on the desktop by grabbing the title bar of the
frame and dragging it with the mouse. You can also resize the frame by dragging
the bottom-right corner of the frame. Note what happens when the frame is made
wider: the second label pops up next to the first label.
Every container is managed by an object called a
layout
manager that determines how the components in the con-
tainer are laid out. The layout manager is consulted when
important things happen to the interface, such as when the
frame is resized.
Unless you specify otherwise, the components in a panel will try to arrange
themselves next to one another in a row, and a component will move down to the
next row only when the width of the panel won't accommodate it. Experiment
with this program to see how the layout manager changes the organization of
the labels as the window size is changed. Layout managers are discussed in more
detail in the Graphics Track sections of Chapter 7.
KEY CONCEPT
Every container is managed by a
layout manager.
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 3.36 What is the difference between a frame and a panel?
SR 3.37 Select the term from the following list that best matches each of the
following phrases:
container, content pane, frame, heavyweight, label, layout manager,
lightweight, panel
a. A component that is used to hold and organize other components.
b. A container displayed in its own window with a title bar.
c. Its primary role is to help organize other components in a GUI; it
must be added to another container to be displayed.
d. This type of component is managed by the underlying operating
system.
e. This type of component is managed by the Java program itself.
f. The part of a frame that displays visible elements.
 
Search WWH ::




Custom Search