Sams Teach Yourself C in 21 Days

(singke) #1
More Java Techniques 761

BD6


<applet
name=”AppletTest”
code=”AppletTest” codebase=”file:/C:/WINDOWS/jws/AppletTest”
width=”200”
height=”100”
align=”Top”
alt=”If you had a java-enabled browser, you would see an applet here.”
>
</applet>

Using an Applet ............................................................................................


Applets are wonderfully fascinating programming topics, and you’ll find many books on
Java that devote themselves entirely to applets. To close off this third day of Java pro-
gramming, here’s a sample Java applet that shows you the fundamentals. This program
displays text in the browser window, and when the user clicks the text the color changes.
Listing B6.8 shows the HTML code used to display this applet. Listing B6.9 shows the
applet code itself. To load this applet, you must edit the <APPLET>tag so it correctly
points to the folder where you have placed AppletTest.class.

LISTINGB6.8 AppletTest.html. The HTML code to display the AppletTest applet
<html>
<body>
Here is the applet:
<applet
name=”AppletTest”
code=”AppletTest” codebase=”file:/C:/WINDOWS/jws/AppletTest”
width=”400”
height=”100”
align=”Top”
alt=”If you had a java-enabled browser, you would see an applet here.”
>
</applet>
</body>
</html>

LISTINGB6.9 AppletTest.java. A simple Java applet
1: import java.applet.Applet;
2: import java.awt.*;
3:
4: public class AppletTest extends Applet {
5: Font f = new Font(“TimesRoman”, Font.BOLD, 36);
6: boolean useRed = true;
7:

41 448201x-Bonus6 8/13/02 11:23 AM Page 761

Free download pdf