Creating an Applet
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
On the File menu, click New Project.
On the New tab, expand the Visual J++ Projects folder and click Web Pages. Then select the Applet on HTML icon.
In the Name box, enter a name for your project.
In the Location box, enter the path where you want to save your project, or click Browse to navigate to the folder.
Click Open. A collapsed view of your project appears in http://msdn.microsoft.com/en-us/library/aa243138(v=vs.60).aspx .
- In Project Explorer, expand the project node. A Java source file with the default name of Applet1.java has been added to your project, as well as an HTML page named Page1.htm.
Note Renaming the Applet1.java 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
To view the source code, double-click Applet1.java in Project Explorer. Program execution begins with the
initmethod simply calls
usePageParams. Use http://msdn.microsoft.com/en-us/library/aa265223(v=vs.60).aspx to locate the
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
- If Class Outline is not displayed, point to Other Windows on the View menu and click Document Outline.
initFormmethod initializes the background and foreground colors, and adds a java.awt.Label control to the applet.
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
usePageParamsis unable to retrieve the
<PARAM>values, then default values are used.
For information about modifying your code in the development environment, see http://msdn.microsoft.com/en-us/library/aa242915(v=vs.60).aspx .
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
On the Build menu, click Build. Any compilation errors or messages appear in the http://msdn.microsoft.com/en-us/library/aa239204(v=vs.60).aspx . (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.
- 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 http://msdn.microsoft.com/en-us/library/aa265386(v=vs.60).aspx 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.