A Java Applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a page. When we use a Java technology-enabled browser to view a page that contains an Applet, the Applet's code is transferred to our system and executed by the browser's Java Virtual Machine (JVM).
In this Java Applet tutorial, we will cover the basics of Applets and Swing.
Applet development involves two basic steps, developing the Java code and running it from a Web browser.
To create an Applet, we must create a class that extends JApplet
from the Swing package. Technically we could extend Applet
from the AWT package, but this tutorial will focus on Swing because it provides a more modern and sophisticated set of graphical user interface (GUI) components.
The class definition for the first Applet we will write is shown below:
When we extend an Applet, we need to implement a method called init
. All GUI initialization is performed in this method. GUI components are represented by classes and to use these components we must create instances of them and add them to our Applet. For example to create a text area and add it to our Applet, we could write an init
method that looks like this: