Java Reference
In-Depth Information
}
}
}
How It Works
The constructor stores its argument value in the data member,
buttonID
, so each listener object will have
the ID for the button available. This is used in the
actionPerformed()
method to select the appropriate
code to execute for a particular button. Each case in the
switch
statement corresponds to a different button.
You could extend this to enable the class to handle as many different buttons as you want by adding case
statements. Because of the way we have implemented the method, each button must have a unique ID
associated with it. Of course, this isn't the only way to do this, as we'll see in a moment.
For the
PICK
_
LUCKY
_
NUMBERS
button, we just call the
getNumbers()
method to produce a set of
numbers, and then call the
setValue()
method for each selection button and pass a number to it. We
will implement the
setValue()
method when we define the selection class in detail, in a moment.
For the
COLOR
button, we create a new color by exclusive ORing (that is, XOR) the
RGB
value of
flipColor
with the current button color. You will recall from our discussion of the
^
operator (in
Chapter 2) that you can use it to exchange two values, and that is what we are doing here.
flipColor
was defined as the two colors,
Color.yellow
and
Color.red
, exclusive ORed together. Exclusive
ORing this with either color will produce the other so we flip from one color to the other automatically
for each button by exclusive ORing the background and
flipColor
. We must get the
RGB
value for
each color and operate on those - you can't apply the
^
operator to the objects. We then turn the
resulting
RGB
value back into a
Color
object.
Let's now add the inner class,
Selection
, which defines the lucky number buttons.
Try It Out - Defining the Selection Buttons
Each button will need to store the value shown on the label, so the class will need a data member for this
purpose. The class will also need a constructor, the
setValue()
method to set the value for the button to a
new value, and a method to compare the current value for a button to a given value. We need to be able to
set the value for a button for two reasons - we call it when we set up all six selections in the listener for the
control button, and we want to reset the value for a button to change it individually.
The method to compare the value set for a button to a given integer will enable us to exclude a number
that was already assigned to a button in the process of generating the button values. We'll also need to
implement the
actionPerformed()
method to handle the action events for the button, as the buttons
are going to handle their own events. Here's the basic code for the class definition:
class Selection extends JButton implements ActionListener {
// Constructor
public Selection(int value) {
super(Integer.toString(value)); // Call base constructor and set the label
this.value = value; // Save the value
setBackground(startColor);
setBorder(BorderFactory.createRaisedBevelBorder()); // Add button border
setPreferredSize(new Dimension(80,20));
addActionListener(this); // Button listens for itself