The Applet Tag

Now that you know a little bit about the Java language it is time to learn how to incorporate Java applets into your normal web pages. The new tag used for displaying applets is the APPLET tag. Here is a simple example of how to run a Java applet in your web page.

<APPLET CODE="Applet.class" WIDTH=150 HEIGHT=150></APPLET>

This simple Applet tag tells a Java capable browser to run the java code Applet.class in a box with width 150 pixels and height 150 pixels.

Here is the full description of the Applet tag:
    '<' 'APPLET'
['CODEBASE' '=' codebaseURL] 'CODE' '=' appletFile ['ALT' '=' alternateText] ['NAME' '=' appletInstanceName] 'WIDTH' '=' pixels 'HEIGHT' '=' pixels ['ALIGN' '=' alignment] ['VSPACE' '=' pixels] ['HSPACE' '=' pixels] '>' ['<' 'PARAM' 'NAME' '=' appletAttribute1 'VALUE' '=' value '>'] ['<' 'PARAM' 'NAME' '=' appletAttribute2 'VALUE' '=' value '>'] . . . [alternateHTML] '</APPLET>' 'CODEBASE' '=' codebaseURL This optional attribute specifies the base URL of the applet -- the directory that contains the applet's code. If this attribute is not specified, then the document's URL is used. 'CODE' '=' appletFile This required attribute gives the name of the file that contains the applet's compiled Applet subclass. This file is relative to the base URL of the applet. It cannot be absolute. 'ALT' '=' alternateText This optional attribute specifies any text that should be displayed if the browser understands the APPLET tag but can't run Java applets. 'NAME' '=' appletInstanceName This optional attribute specifies a name for the applet instance, which makes it possible for applets on the same page to find (and communicate with) each other. 'WIDTH' '=' pixels 'HEIGHT' '=' pixels These required attributes give the initial width and height (in pixels) of the applet display area, not counting any windows or dialogs that the applet brings up. 'ALIGN' '=' alignment This required attribute specifies the alignment of the applet. The possible values of this attribute are the same as those for the IMG tag: left, right, top, texttop, middle, absmiddle, baseline, bottom, absbottom. 'VSPACE' '=' pixels 'HSPACE' '=' pixels These option attributes specify the number of pixels above and below the applet (VSPACE) and on each side of the applet (HSPACE). They're treated the same way as the IMG tag's VSPACE and HSPACE attributes. '<' 'PARAM' 'NAME' '=' appletAttribute1 'VALUE' '=' value '>' . . . This tag is the only way to specify an applet-specific attribute. Applets access their attributes with the getParameter() method.
The alternateHTML line means that if a browser is not Java aware this set of HTML code will be viewed instead of the applet.

So let's examine a more complex Applet tag:

<APPLET CODE="Counter.class" WIDTH=150 HEIGHT=50>
<PARAM name="start" value="1">
<PARMA name="end" value="10">
<BLOCKQUOTE>
<HR>
You need a java capable browser to see this nifty Java applet.
<HR>
</BLOCKQUOTE>
</APPLET>

This tag describes an applet named Counter.class, width 150 pixels and height 50 pixels. Two parameters are sent to the applet, one named 'start' with the value '1' and the other named 'end' with the value '10'. A Java capable browser should skip the HTML in the BLOCKQUOTE tag but a non-Java capable browser should skip the APPLET tag but display the BLOCKQUOTE information.


Prev Subject Next Subject