Table of Contents |
By following the steps on this page, you can create and use an applet. If you aren't interested in applets, you might want to skip ahead to the Writing Java Programs trail.
Create a Java Source File
Create a file namedHelloWorld.java
with the Java code shown here:import java.applet.Applet; import java.awt.Graphics; public class HelloWorld extends Applet { public void paint(Graphics g) { g.drawString("Hello world!", 50, 25); } }Compile the Source File
Compile the source file using the Java compiler.If compilation succeeds, the compiler creates a file named
HelloWorld.class
. If compilation fails, make sure you typed in and named the program exactly as shown above.Create an HTML File that Includes the Applet
Create a file namedHello.html
in the same directory that containsHelloWorld.class
. This HTML file should contain the following text:<HTML> <HEAD> <TITLE> A Simple Program </TITLE> </HEAD> <BODY> Here is the output of my program: <APPLET CODE="HelloWorld.class" WIDTH=150 HEIGHT=25> </APPLET> </BODY> </HTML>Load the HTML File
Load the HTML file into an application that can run Java applets. This application might be a Java-compatible browser or another Java applet viewing program, such as the Applet Viewer provided in the JDK. To load the HTML file, you usually need to tell the application the URL of the HTML file you've created. For example, you might enter something like the following into a browser's URL or Location field:file:/home/kwalrath/HTML/Hello.htmlOnce you've successfully completed these steps, you should see something like this in the browser window:
Now What?
Now you can:
- Continue on in this lesson to learn about the anatomy of applets and about importing classes.
- Return to the Trail Map to get an overview of the trails you can follow.
- Learn more about writing applets by going to the Writing Applets trail.
Table of Contents |