For many element tag pairs, you can specify an element attribute in the starting tag that defines additional or qualifying data about the element. This is how a Java applet is identified in an < applet > tag. Here is an example of how you might include a Java applet in an HTML document:< /applet >
< html >
< head >
< title > A Simple Program < /title >
< /head >
< body >
< hr/ >
< applet code = “MyFirstApplet.class” width = 300 height = 200 >
< /applet >
< hr/ >
< /body >
< /html >
The two shaded lines between tags for horizontal lines specify that the bytecodes for the applet are contained in the file MyFirstApplet.class. The name of the file containing the bytecodes for the applet is specified as the value for the code attribute in the < applet > tag. The other two attributes, width and height, define the width and height of the region on the screen that will be used by the applet when it executes. These always have to be specified to run an applet. Here is the Java source code for a simple applet:
import javax.swing.JApplet;
import java.awt.Graphics;
public class MyFirstApplet extends JApplet {
public void paint(Graphics g) {
g.drawString(“To climb a ladder, start at the bottom rung”, 20, 90);
}
}
Note that Java is case-sensitive. You can’t enter public with a capital P - if you do, the program won’t compile. This applet just displays a message when you run it. The mechanics of how the message gets displayed are irrelevant here - the example is just to illustrate how an applet goes into an HTML page. If you compile this code and save the previous HTML page specification in the file MyFirstApplet.html in the same directory as the Java applet code, you can run the applet using appletviewer from the JDK with the command:
appletviewer MyFirstApplet.html
In this particular case, the window is produced by Internet Explorer under Windows XP. Under other operating systems and browsers it is likely to look a little different. Since the height and width of the window for the applet are specified in pixels, the physical dimensions of the window will depend on the resolution and size of your monitor. This example should work by default with Internet Explorer since the installation process for the JDK will install the Java plug-in for you. If it doesn’t work, check the Internet Options . . . on the Tools menu for Internet Explorer. On the Advanced tab you should find an option titled “Use JRE v1.5.0 for < applet > (requires restart)”; make sure this option is checked. If you use Mozilla 1.x or Netscape 7.x, follow the instruction given in the installation documentation for the JDK to enable the plug-in.