|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Before you close the <APPLET> tag, you can include some HTML. This HTML will only be displayed if the browser did not understand the <APPLET> tag, so you should display a message telling the user what he or she is missing. The HTML in Listing 37.3 (lines 9 through 13) tells the user about the problem, puts up a graphic showing what the applet looks like, and then offers a link to the Netscape site so the end user can download Netscape Communicator. Running Your Applet with AppletViewerTo start AppletViewer, type appletViewer HelloApplet.html at the command line. Figure 37.5 shows our applet in action. It doesnt do much, but it works!
Seeing Your Applet in a Web BrowserEventually youll want your applet to appear on a Web page. Figure 37.6 shows HelloApplet viewed with Microsoft Internet Explorer. Figure 37.7 shows the same applet in Netscape Navigator.
Step by Step Through the CodeThis section walks through the code for the HelloApplet class so you can see why each line is written the way it is. Even though only one executable statement exists (in line 8), this tiny program illustrates many of the principles youll use in writing any Java applet.
Importing PackagesThe first two lines of HelloApplet.java tell the compiler to use class definitions from two specific packages: java.applet and java.awt. (AWT stands for the Abstract Windowing Toolkit.) Specifically, these directives tell the compiler to use the Applet object from java.applet and the Graphics object from java.awt. The import statement is simply a shorthand notation. We could have written public class HelloApplet extends java.applet.Applet and public void paint (java.awt.Graphics theGraphics) but most programmers prefer the aesthetics of import. If we planned to use many classes from java.applet or java.awt, we could have written import java.applet.*; import java.awt.*; to give the compiler permission to use any class from those packages. In practice, this asterisk notation is used frequently, although we lose the immediate capability to identify where a class is defined. If we didnt know Java well, we wouldnt know whether the Graphics class was part of java.applet or java.awt.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |