ptg7068951
272 HOUR 19:Creating a Threaded Program
Starting with init()
The init()method of an applet automatically is handled once when the
applet first starts to run. This method is used to assign values to the arrays
pageTitleand pageLink. It also is used to create a clickable button that
appears on the applet. The method consists of the following statements:
public voidinit() {
pageTitle = new String[] {
“Sun’s Java site”,
“Cafe au Lait”,
“JavaWorld”,
“Java in 24 Hours”,
“Sams Publishing”,
“Workbench”
};
pageLink[0] = getURL(“http://java.sun.com”);
pageLink[1] = getURL(“http://www.ibiblio.org/javafaq”);
pageLink[2] = getURL(“http://www.javaworld.com”);
pageLink[3] = getURL(“http://www.java24hours.com”);
pageLink[4] = getURL(“http://www.samspublishing.com”);
pageLink[5] = getURL(“http://workbench.cadenhead.org”);
Button goButton = new Button(“Go”);
goButton.addActionListener(this);
FlowLayout flow = new FlowLayout();
setLayout(flow);
add(goButton);
}
The title of each page is stored in the six elements of the pageTitlearray,
which is initialized using six strings. The elements of the pageLinkarray
are assigned a value returned by the getURL()method, yet to be created.
The last seven statements of the init()method create and lay out a button
labeled “Go” in the applet window.
Catching Errors as You Set Up URLs
When youset up a URLobject, you must make sure the text used to set up
the address is in a valid format. http://workbench.cadenhead.organd
http://www.samspublishing.comare valid, but http:www.javaworld.com
would not be because of missing “/” marks.
The getURL(String)methodtakes a web address as an argument, return-
ing a URLobject representing that address. If the string is not a valid
address, the method returns nullinstead: