ptg7068951
276 HOUR 19:Creating a Threaded Program
Handling Mouse Clicks
The last thing to take care of in the LinkRotatorapplet is event handling.
Whenever a user clicks the Go button, the web browser should open the
website shown. This is done with a method called actionPerformed(),
which is called whenever the button is clicked.
The following is the actionPerformed()method of the LinkRotator
applet:
public voidactionPerformed(ActionEvent event) {
if (runner!= null) {
runner= null;
}
AppletContext browser = getAppletContext();
if (pageLink[current] != null) {
browser.showDocument(pageLink[current]);
}
}
The first thing that happens in this method is that the runnerthread is
stopped. The next statement creates a new AppletContextobject called
browser.
An AppletContextobject represents the environment in which the applet
is being presented—in other words, the page it’s located on and the web
browser that loaded the page.
The showDocument(URL)method loads the specified web address in a
browser. If pageLink[current]is a valid address, showDocument()
requests that the browser load the page.
Displaying Revolving Links
You’re now ready to create the program and test it. Create a new empty
Java file named LinkRotatorand type in the text from Listing 19.2.
LISTING 19.2 The Full Text of LinkRotator.java
1: importjava.applet.*;
2: importjava.awt.*;
3: importjava.awt.event.*;
4: importjavax.swing.*;
5: importjava.net.*;
6:
7: public classLinkRotator extendsJApplet
8: implementsRunnable, ActionListener {