ptg7068951
Completing a Graphical Application 207
JButton finishButton = new JButton(“Finish”);
previousButton.setEnabled(false);
This method is an effective way to prevent a component from sending a user
event when it shouldn’t. For example, if you’re writing a Java application that
collects a user’s address using text fields, you could disable a Save Address
button until the user provided a street address, city, state and ZIPcode.
Completing a Graphical Application
To see how Swing’s event-handling classes work in a Java program, you
finish LottoMadness, the lottery simulation begun during Hour 14,
“Laying Out a User Interface.”
At this point, LottoMadnessis just a GUI. You can click buttons and enter
text into text boxes, but nothing happens in response. In this workshop,
you create LottoEvent, a new class that receives user input, conducts lotto
drawings, and keeps track of the number of times you win. When the class
is complete, you add a few lines to LottoMadnessso that it makes use of
LottoEvent. It often is convenient to divide Swing projects in this manner,
with the GUI in one class and the event-handling methods in another.
The purpose of this application is to assess the user’s chance of winning a
six-number lotto drawing in a lifetime. Figure 15.2 shows a screen capture
of the program as it runs.
FIGURE 15.2
Running the LottoMadness
application.