Page 681 - Introduction to Programming with Java: A Problem Solving Approach
P. 681
16.3 A Simple Window Program 647
user fired listener event action event handler
------------------
-----------------
-------------------
---------------
------------------
------------------
-----------------
OK
Figure 16.2 What happens when a button is pressed
works; you just have to understand it well enough to use it. For example, you have to know how to plug in your
event handlers properly. Figure 16.3 provides a high-level, graphic illustration of what we’re talking about.
= developer’s code (event handlers, etc.)
Apago P nhancer
Framework
DF E
Figure 16.3 Event-driven programming framework
Why did Sun bother to provide the event-driven programming framework? It satisfies the goal of get- ting maximum benefit from minimum code. With the help of the framework, Java programmers can get a GUI program up and running with a relatively small amount of effort. Initially, the effort might not seem so small, but when you consider all that the GUI program does (automatic event firing, listening for fired events, and so on), you’ll find that your return on investment is quite good.
16.3 A Simple Window Program
OK. Enough talk about concepts. Time to roll up your sleeves and get your hands dirty with some code. To get a feel for the big picture, let’s start with a simple GUI program and discuss the GUI commands at a high level. Later, we’ll cover the GUI commands in greater detail.
In Figure 16.4, we present a SimpleWindow program that displays a line of text inside a window. Note the two import statements at the top of the program. They import the javax.swing and java.awt packages. In writing GUI programs, you’ll use many of Java’s pre-built GUI classes from Sun’s API library.