Java Reference
In-Depth Information
Chapter 7
Graphical User Interfaces
7.1 Introduction
In the previous chapter we introduced Java graphics and focused on the display
of components, drawings, and images. Here we discuss how to build a graphical
display that interacts with the user. A Graphical User Interface (GUI) requires
dynamic components like buttons and menus that cause something to happen
when the user activates them with the mouse or keyboard. These components
generate messages called events that signal what action occurred. We will first
look in this chapter at the underlying structure for generating and processing
events.
We then introduce several more Swing components such as checkboxes and
sliders and present demonstration programs that illustrate how to receive and
process the events they generate. We discuss in more detail how components are
arranged on the interface with the use of layout managers. For event handling
and other tasks, we examine the use of inner classes and adapter classes. After a
discussion of frames and menu bars, we demonstrate GUI construction and event
techniques with a couple of programs involving histograms.
7.2 Events
The graphical user interface (GUI) offers a profoundly different programming
environment than the old step-by-step, linear world of a procedural program
communicating via a console. In the latter approach, the user simply starts a
program and waits for it to churn through its algorithm and eventually reach the
end and stop. In the GUI environment, the program instead waits for the user to
select some task and then carries out that selected operation. When it finishes the
operation, the program returns to a wait state. Furthermore, with thread processes
(see Chapter 8) the program can carry on multiple operations and interactions
with the user, all appearing to happen simultaneously even though they typically
runinasingle processor.
The JVM communicates with the operating system to monitor the program's
interface. When the user moves a mouse, clicks one of its buttons, hits a key,
201
Search WWH ::




Custom Search