Java Reference
In-Depth Information
By displaying the six selected numbers on buttons,
we can provide for changing one of the choices by
processing the action event for that button. Thus,
clicking a button will provide another number. We'll
also add a couple of control buttons, one to make a
new selection for a complete set of lottery numbers,
and another just for fun to change the button color.
Here's how the applet will look when running under
appletviewer :
Try It Out - A Lottery Applet
We can outline the broad structure of the applet's code as follows:
// Applet to generate lottery entries
import javax.swing.*;
import java.awt.*;
import java.util.Random; // For random number generator
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Lottery extends JApplet {
// Initialize the applet
public void init() {
// Set up the lucky numbers buttons...
// Set up the control buttons...
}
// Custom button showing lottery selection
// Each button listens for its own events
class Selection extends JButton implements ActionListener {
// Constructor
public Selection(int value) {
// Create the button showing the value...
}
// Handle selection button event
public void actionPerformed(ActionEvent e) {
// Change the current selection value to a new selection value
}
// Details of the rest of the selection class definition...
}
// Class defining a handler for a control button
class HandleControlButton implements ActionListener {
// Constructor...
Search WWH ::




Custom Search