Starting with init()

The init() method of an applet automatically is handled once when the applet first starts to run. In this example, this method is used to assign values to the two arrays created for this applet, pageTitle and pageLink. It also is used to create a clickable button that will appear on the applet. The method consists of the following statements:

public void init() {
 pageTitle[0] = "Sun's Java site";
 pageLink[0] = getURL("http://java.oracle.com");
 pageTitle[1] = "Cafe au Lait";
 pageLink[1] = getURL("http://www.ibiblio.org/javafaq");
 pageTitle[2] = "JavaWorld";
 pageLink[2] = getURL("http://www.javaworld.com");
 pageTitle[3] = "Java 2 in 24 Hours";
 pageLink[3] = getURL("http://www.java24hours.com");
 pageTitle[4] = "Sams Publishing";
 pageLink[4] = getURL("http://www.127.0.0.1");
 pageTitle[5] = "Workbench";
 pageLink[5] = getURL("http://www.cadenhead.org/workbench");
 Button goButton = new Button("Go");
 goButton.addActionListener(this);
 FlowLayout flow = new FlowLayout();
 setLayout(flow);
 add(goButton);
}


Strings are assigned to the six elements of the pageTitle array, which stores the title of each web page. The elements of the pageLink array are assigned a value returned by the getURL() method, which you will be creating for this program. The last seven statements of the init() method are used to create a button and place it on the applet window. The button has the name goButton and is labeled with the text Go.

      
Comments