Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

Creating an Applet

Testing the SalutonAppletProgram


Java applets can’t be run like applications because they lack a main()
method.


Instead, to run an applet, you must add markup to a web page that con-
tains the applet. To create an example web page for SalutonApplet, create
a new web page in NetBeans by following these steps:



  1. Choose File, New File. The New File dialog opens.

  2. Choose Other from the Categories pane and HTMLFile from File
    Ty p e s , t h e n c l i c k N e x t. T h e N e w H T M LFile dialog opens.

  3. Give the file the name SalutonAppletand click Finish.


NetBeans opens the source code editor with some default HTMLmarkup.
Delete all the markup that’s been provided for you, enter Listing 17.2 into
the editor, and save the file.


LISTING 17.2 The Full Text of SalutonApplet.html
1:
2:
3: Saluton Mondo!
4:
5: <bodybgcolor=”#000000”text=”#FF00FF”>
6:

This is a Java applet.


7: <applet
8: code=”SalutonApplet.class”
9: codebase=”..\..\build\classes”
10: height=”150”
11: width=”300”
12: >
13:

You need a Java-enabled browser to see this.


14:
15:
16:


Thetag is defined in lines 7–14, but line 13 will be ignored in any
browser equipped to run Java.


After saving the file, you can view it in aweb browser: In the Project pane
to the left of the source code editor, right-click the filename
SalutonApplet.html, and then choose View. The web page opens in your
computer’s default web browser, as shown in Figure 17.1.