(^394) | Event-Driven Input and Output
(^129) Increment
//Event handler method
public void actionPerformed(ActionEvent event)
{
number++;
numberLabel.setText("" + number);
}
Figure 8.5 An Event Loop
handling events from multiple buttons, we can add a “Done” button for which we program a
separate check.
When you write an event-handling class, it is important to be conscious of any event loops
that you create. Because they are coded implicitly, it is easy to overlook the fact that they are
even present. The danger arises when you create an event loop that doesn’t have an exit con-
dition. Then the only way to end the loop is to issue an operating system command to stop
the application. For example, if we had forgotten to set the default closing operation, we
would have no way out of the event loop. In our code, it is not at all obvious that this action
is what ends the loop.
8.4 Entering Data Using Fields in a Frame
Figure 8.6 shows a frame that might be used for entering data into a company
payroll application. This frame contains three labels and a button (Done) in the left
column of its grid. The right column has three data entryfields.
It’s important to clarify that a data entry field and a Java field (a declaration
in a class) are not the same. One is an area on the screen, while the other is a
name representing a constant or variable. In this case, the Java designers used
Field A component of a frame
in which the user can type a
value; the user must first place
the cursor in the field by clicking
inside the field