ptg7068951
212 HOUR 15:Responding to User Input
The LottoEventclass has one constructor: LottoEvent(LottoMadness).
The LottoMadnessobject specified as an argument identifies the object that
is relying on LottoEventto handle user events and conduct drawings.
The following methods are used in the class:
. TheclearAllFields()method causes all text fields in the applica-
tion to be emptied out. This method is handled when the user clicks
the Reset button.
. TheaddOneToField()method converts a text field to an integer,
increments it by one, and converts it back into a text field. Because all
text fields are stored as strings, you have to take special steps to use
them in expressions.
. ThenumberGone()method takes three arguments—a single number
from a lotto drawing, an array that holds several JTextFieldobjects,
and a countinteger. This method makes sure that each number in a
drawing hasn’t been selected already in the same drawing.
. ThematchedOne()method takes two arguments—a JTextField
object and an array of six JTextFieldobjects. This method checks to
see whether one of the user’s numbers matches the numbers from
the current lotto drawing.
The application’s actionPerformed()method receives the action events
when the user clicks a button. The getActionCommand()method retrieves
the label of the button to determine which component was clicked.
Clicking the Play button causes the startPlaying()method to be called.
This method disables four components. Clicking Stop causes the
stopPlaying()method to be called, which enables every component
except for the Stop button.
The itemStateChanged()method receives user events triggered by the
selection of the Quick Pick or Personal check boxes. The getItem()
method sends back an Objectthat represents the check box that was
clicked. If it’s the Quick Pick check box, six random numbers from 1 to 50
are assigned to the user’s lotto numbers. Otherwise, the text fields that
hold the user’s numbers are cleared out.
The LottoEventclass uses numbers from 1 to 50 for each ball in the
lotto drawings. This is established in Line 113, which multiplies the
Math.random()method by 50, adds 1 to the total, and uses this as an argu-
ment to the Math.floor()method. The end result is a random integer
from 1 to 50. If you replace 50 with a different number here and on Line 65,