A Simple Applet
Home ] Up ] Introduction to Applets ] [ A Simple Applet ] HTML Tags for Applets ] The Applet Life Cycle ] Multimedia and URLs ] Applet Context ] Converting Applications to Applets ]

 

 

Here's a trivial applet:

package applets;

import java.awt.Container;

import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class GreetApplet extends JApplet
{
  public void init()
  {
    Container contentPane = getContentPane();
    JLabel label = 
        new JLabel("Greetings from a Java applet",
                   SwingConstants.CENTER);
    contentPane.add(label);
  }
}

...and here's what it looks like on this page:

Bad luck! Your browser is not Java enabled. You should be seeing an applet here. Please ensure that you are using the Java Plug-in, preferably the latest version, minimum Java 1.4 .

Note: Versions are important when writing applets. 

For example, when I first wrote the above applet, I wrote it and compiled it using JDK 1.5 .  However, when I tried it in one of my browsers that had a Java 1.4 plug-in, my browser failed to load the applet successfully.  I looked in the Java Plug-in's Console, and it told me that it had encountered an error:

java.lang.UnsupportedClassVersionError: applets/GreetApplet 
    (Unsupported major.minor version 49.0)

Self-descriptive message, don't you think?

When I investigated further, I discovered that I could not run the code I had written under JDK 1.5 using a Java Plug-in 1.4 (or earlier).  So, I changed the source code to ensure I wasn't using any JDK 1.5 specific features, and then compiled it under JDK 1.4.  The resulting version now runs using both the 1.4 and 1.5 versions of the Java Plug-in.

 

This page was last modified on 02 October, 2007