Tutorial: Building an applet
The Applet wizard creates a .java
file and an applet HTML file and places them in the project you just created with the Project wizard.
To generate these source files for your applet, follow these steps:
firstapplet
, in Step 1. By default, the wizard takes the package name from the project file name, FirstApplet.jpr
.
firstapplet.GoodEveningApplet.class
. The class file is saved in the following Java package structure: firstapplet/GoodEveningApplet.class
.
java.applet.Applet
.
javax.swing.JApplet
, your applet won't run in the browsers. Swing is not yet supported by the browsers.
Step 1 of the Applet wizard should look like this:
PARAM
tags inside the APPLET
tags in the applet's HTML file
and also inserts code for handling parameters in the source code. Applet parameters, the equivalent of command-line arguments for applications, allow you to customize your applet. Do not add any parameters.
Step 3 of the Applet wizard should look like this:
Some of the following attributes are found in the APPLET
tag of the HTML file:
"."
specifies the same directory as the HTML file running the applet. The CODEBASE
attribute is required when the class files are in a different directory than the HTML file.
init()
method. You'll see it in the HTML file when it is generated.
CODEBASE
.
CODEBASE
, CODE
, ARCHIVE
, and NAME
must be in quotation marks and are case-sensitive.
Note that two files are created and added to the project, GoodEveningApplet.java
and GoodEveningApplet.html
. Double-click each file and select the Source tab in the content pane to view the generated code.
Look at the .java
file and note the following:
init()
method. The applet HTML file must call the class that contains the init()
method for the applet to run.
firstapplet
is the first line of code. The class file will be saved in a firstapplet
directory according to Java conventions.
import java.awt.*; import java.awt.event.*; import java.applet.*;
Look at the HTML file and notice that the wizard inserted the CODE value, firstapplet.GoodEveningApplet.class
.
Note: By default, JBuilder saves the source files to:
/[home]/jbproject/FirstApplet/src/firstapplet
In this tutorial, the applet HTML file is also saved to the src
directory.
The class files, after compiling, are saved to: /[home]/jbproject/FirstApplet/classes/firstapplet
Java always follows the package hierarchy when saving files. In this example, the source and class files are saved within a firstapplet
directory on the source and output paths to reflect the firstapplet
package structure. These paths are set for the project in the Project Properties dialog box. In this example, we accepted the default JBuilder paths.
Tutorial: Building an applet
GoodEveningApplet.html
in the project pane and select Run. This runs your applet in Sun's appletviewer.
You can change the applet's run settings on the Run page of the Project Properties dialog box. To access this dialog box, select Project|Project Properties or right-click FirstApplet.jpr
and select Properties.
init()
method, not from the .java
file. Any attempt to run the .java
file results in an error message (unless the Can Run Standalone option was selected in Step 1 of the Applet wizard):
When you run your applet, the message pane appears at the bottom of the AppBrowser where any compile-time errors are displayed. Correct any errors and run the applet again.java.lang.NoSuchMethodError: main Exception in thread "main"
Your applet is displayed and should look like this: