Running an JApplet

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
Historical topic: Java appletsModern web browsers do not run Java applets. The Applet API was deprecated for removal before Java 25 and was removed in Java 26. Treat applet examples on this page as historical object-oriented/GUI examples; for new desktop interfaces use Swing where appropriate or JavaFX/OpenJFX.

Answer:

Applets are not stand-alone objects, and only run in the context of a larger program (such as a browser).

Running an JApplet

An applet does not have a main() method and so cannot be started as an application. For the applet to do anything, some other application must be running and call the applet's methods. A Web browser can do this.

A Web page is a text file that a Web browser can display. The browser follows instructions that are part of the text to nicely format the page. Here is a tiny Web page that asks for the applet AnotherHello:

<html>
<body>
<applet code="AnotherHello.class" width="300" height="150">
</applet>
</body>
</html>

This tells the browser to run the applet AnotherHello.class, and to use a 300 pixels wide and 150 high drawing area. Quote marks are required around all the values, including the numbers, but usually web browsers will work if you forget them.

You can create this Web page by typing the above characters into a text editor (or copy and paste them). Then save the file as AnotherHello.html in the same directory that has AnotherHello.class. Your directory should look something like this:

directory listing

Now double click on AnotherHello.html. Your default Web browser should start running and display the applet.

QUESTION 8:

Fill in the blanks in the following HTML file so that it asks to run the applet Poem in a window that is 250 pixels wide and 200 pixels high.

<html>
<body>
<applet code=""  width="" height="">
</applet>
</body>
</html>