Java Reference
In-Depth Information
You're ready to start generating lottery entries. If you compile the Lottery.java file, you can run the
applet using appletviewer . You need an HTML file, of course. The following contents for the file do
the job:
<html>
<head>
</head>
<body bgcolor="000000">
<center>
<applet
code = "Lottery.class"
width = "300"
height = "200"
>
</applet>
</center>
</body>
</html>
You can adjust the width and height values to suit your monitor resolution if necessary.
The applet should produce a selection each time you click the left control button. Clicking any of the
selection buttons generates an action event that causes a new value to be created for the button. This en-
ables you to replace any selection that you know to be unlucky with an alternative.
NOTE Undoubtedly, anyone who profits from using this applet will have immense
feelings of gratitude and indebtedness towards the author, who will not be offended
in the slightest by any offers of a portion of that success, however large!
Alternative Event-Handling Approaches
As I indicated in the discussion, there are various approaches to implementing listeners. Let's look at a
couple of other ways in which you could have dealt with the control button events.
Instead of passing a constant to the listener class constructor to identify which button was selected, you
could have exploited the fact that the event object has a method, getSource() , that returns a reference to
the object that is the source of the event. To make use of this, references to both button objects would need
to be available to the actionPerformed() method to enable you to determine which button was clicked.
You could easily arrange for this to be the case by adding a couple of fields to the Lottery class:
JButton pickButton = new JButton("Lucky Numbers!");
JButton colorButton = new JButton("Color");
The inner class could then be defined as follows:
class HandleControlButton implements ActionListener {
// Handle button click
Search WWH ::




Custom Search