Java Reference
In-Depth Information
Most of the example programs we've looked at so far have been Java applica-
tions. More specifically, they have been command-line applications, which inter-
act with the user only through simple text prompts. A Java application can have
graphical components as well. Throughout the rest of the topic, in the Graphics
Track sections at the end of each chapter, we will explore the capabilities of Java
to create programs with graphical user interfaces (GUIs). In this chapter we estab-
lish the basic issues regarding graphics-based applications.
A GUI component is an object that represents a screen element that is used to
display information or to allow the user to interact with the program in a certain
way. GUI components include labels, buttons, text fields, scroll bars, and menus.
Java components and other GUI-related classes are defined primarily in two
packages: java.awt and javax.swing . (Note the x in javax.swing .) The Abstract
Windowing Toolkit (AWT) was the original Java GUI package. It still contains many
important classes, such as the Color class that we used in Chapter 2. The Swing pack-
age was added later and provides components that are more versatile than those of
the AWT package. Both packages are needed for GUI development, but we will use
Swing components whenever there is an option.
A container is a special type of component that is used
to hold and organize other components. Frames and panels
are two examples of Java containers. Let's explore them in
more detail.
KEY CONCEPT
Containers are special GUI compo-
nents that hold and organize other
components.
Frames and Panels
A frame is a container that is used to display GUI-based Java applications. A
frame is displayed as a separate window with its own title bar. It can be repo-
sitioned on the screen and resized as needed by dragging it with the mouse. It
contains small buttons in the corner of the frame that allow the frame to be mini-
mized, maximized, and closed. A frame is defined by the JFrame class.
A panel is also a container. However, unlike a frame, it cannot be displayed
on its own. A panel must be added to another container for
it to be displayed. Generally a panel doesn't move unless you
move the container that it's in. Its primary role is to help
organize the other components in a GUI. A panel is defined
by the JPanel class.
We can classify containers as either heavyweight or lightweight. A heavyweight
container is one that is managed by the underlying operating system on which the
program is run, whereas a
KEY CONCEPT
A frame is displayed as a separate
window, but a panel can be displayed
only as part of another container.
lightweight container is managed by the Java program
itself. Occasionally this distinction will be important as we explore GUI develop-
ment. A frame is a heavyweight component, and a panel is a lightweight component.
 
Search WWH ::




Custom Search