Creating an Applet

Visual Studio 6.0

An applet runs from within an HTML page, and is created with the classes in the . Your applet must extend java.applet.Applet, and typically uses the classes in the java.awt package to provide a graphical user interface.

Note   Before you use the following procedure to create an applet, close any projects that you may already have open. (On the File menu, click Close All.)

To create an applet

  1. On the File menu, click New Project.

  2. On the New tab, expand the Visual J++ Projects folder and click Web Pages. Then select the Applet on HTML icon.

  3. In the Name box, enter a name for your project.

  4. In the Location box, enter the path where you want to save your project, or click Browse to navigate to the folder.

  5. Click Open. A collapsed view of your project appears in .

  6. In Project Explorer, expand the project node. A Java source file with the default name of has been added to your project, as well as an HTML page named Page1.htm.

Note   Renaming the file does not rename the associated class in the source code, and vice versa. You must manually change all instances of the old name. (Note that you can create an empty project and then add a class with the Class template. This two-step process allows you to name the class before it is created; however, the Class template does not provide the basic code framework for an applet.)

Viewing Code in the Text Editor

The Applet template already provides some sample functionality. You can view this sample code in the Text editor.

To view the sample code in the applet

  1. To view the source code, double-click in Project Explorer. Program execution begins with the init method.

  2. The init method simply calls initForm and usePageParams. Use to locate the initForm method:
    • If Class Outline is not displayed, point to Other Windows on the View menu and click Document Outline.

    • Expand your class in Class Outline and double-click initForm. The insertion point immediately moves to the initForm method.
  3. The initForm method initializes the background and foreground colors, and adds a java.awt.Label control to the applet.

  4. In Class Outline, double-click usePageParams. This method retrieves the value of the <PARAM> tags from the associated HTML page, and sets the background color, foreground color, and label text to these values. If usePageParams is unable to retrieve the <PARAM> values, then default values are used.

For information about modifying your code in the development environment, see .

Building and Running the Applet

After you build your applet, you can launch the associated HTML page from the development environment or simply open the HTML page in your browser.

To build and run your applet

  1. On the Build menu, click Build. Any compilation errors or messages appear in the . (Double-clicking an error in the Task List moves the insertion point in the Text editor to the error.) Correct the errors and rebuild your application.

  2. To run the applet from the development environment, click Start on the Debug menu. Internet Explorer is launched and Page1.htm displays the applet.

Note   To run the applet without an HTML page, you can use from the command line. At the command prompt, type jview /a Applet1 from the directory location of your project. When the /a option is specified, the Applet Viewer is launched to display your applet.