Java Reference
In-Depth Information
Figure 14.1
HelloWorldApplet displayed in a Web browser.
An applet can actually be embedded within any other application, not just
a Web browser. If your applet is going to be embedded in a Web page, it
must extend the Applet class. If your applet is going to be embedded in
some other application, extending Applet is not required. That being said,
I have never seen an applet whose class did not extend either Applet or
JApplet, so I assume that not extending Applet is rarely done.
Swing Applets
The javax.swing.JApplet class is for creating an applet that uses Swing com-
ponents. The JApplet class is a child of Applet, so JApplet inherits all the meth-
ods of Applet. Web browsers do not distinguish between a regular applet and
a Swing applet. The purpose of the JApplet class is to provide support for
Swing.
Probably the biggest difference between an applet and a JApplet is how
components are added to them. A JApplet has three panes, much like a
JFrame, and components are added to the content pane of the JApplet
(as opposed to an applet where components are added directly to the
Applet). The content pane of a JApplet has BorderLayout by default.
The following HelloSwingApplet extends JApplet. The event handling is
done by using a class named FourColors. Study these two classes and try to
determine what the applet does.
Search WWH ::




Custom Search