Most of our lablets--indeed, most Java programs--take
advantage of Java's facilities for producing graphical, user-friendly
interfaces based on widgets and containers. Java's support for GUI
building blocks doesn't stop there, though. Built into every applet are
the methods needed to make our widgets respond to a full range of interface
events. In this chapter, we use two lablets to illustrate how one writes
programs that not only display the interface elements that we want on
the screen, but then actually respond to our button clicks, key presses,
and mouse movements.
The
first lablet,
GalaEvents
, is an extension of our lablet
from Chapter 3, Gigobite
. It demonstrates how to add
event handlers to an existing applet so that the applet recognizes and
responds to interface events. GalaEvents
doesn't do
much in response to these events, but at least it recognizes them.
The second lablet, SketchPad
, is a more complete example
that not only recognizes interface events, but responds to them in
interesting and useful ways.
![]() |
In this lab, you will: | ||
![]() |
Run and experiment with
the |
||
![]() |
Explore the code of
|
||
![]() |
Run and experiment with
the |
||
![]() |
Change |
||
![]() |
Extend |
SodaPop
to hide the event-handling. The result,
unfortunately, was a program that was nowhere as clean as it could have
been. Go back to SodaPop
and make the event handling as
tidy as possible.
SketchPad
that allows the
user to set the drawing mode to produce curves, as it does now, ovals, or
rectangles. For example, in oval mode, dragging the mouse and then releasing
the mouse button would draw an oval bounded by the rectangle whose opposite
corners are determined by the start and end of the dragged curve, like this:![]() |