Java Reference
In-Depth Information
Applet s and JApplets
Though an applet must have a GUI, its containing class does not extend JFrame . This
is hardly surprising, of course, since applets pre-date the Swing classes, but the
applet's containing class does not extend class Frame either. Before the Swing
classes appeared, an applet consisted of a class that extended class Applet (from
package java.applet ). The introduction of the Swing classes brought in class JApplet
(package javax.swing ), which extends class Applet and makes use of the other Swing
classes. Thus, later applets should extend class JApplet . Unfortunately, there are
major differences of operation between applets that use only pre-Swing classes and
those that use the Swing classes. 'Differences in operation' is actually putting it very
mildly. A lot of Swing applets will simply not work in some of the earlier versions of
Internet Explorer and Netscape! However, this problem has been eradicated in the
latest versions of the major browsers, as will be seen in the next section. Since the
Swing classes have been around for so long now, most of what follows will refer to
Swing applets only, and the term 'applets' will be used without qualifi cation. In
12.4.1 , two pre-Swing examples are used, but these are identifi ed as such explicitly.
Applet Basics and the Development Process
When developing applets, it can be quite tedious having to go into and out of a Web
browser in order to access the Web page containing the applet as changes are made
to that applet. In recognition of this fact, Oracle provides a utility program called
the appletviewer as part of the Java SE. This utility executes an applet when an
HTML document containing the applet is opened by the program. The appletviewer
itself is executed from a command window and must be supplied with the name
of the appropriate HTML fi le as a command line parameter. For example:
appletviewer example.html
Class Applet (and, through it, class JApplet ) extends Panel , rather than class
Frame . The fact that an applet is a Panel object is a deliberate design decision,
related to security. This means that an applet looks like part of an HTML page,
rather than a standalone application. For example, the size of the applet window is
fi xed. This prevents programmers from spoofi ng users. If an applet were an extension
of a JFrame , it could be made to resemble an application residing on a client's
system that could then accept data from the user and transmit it to its host system.
Though a frame window can be created from within an applet (simply by instantiating
class JFrame ), the browser adds a warning message to any such window.
Applets can respond to events, but do not have a main method to drive them.
Instead, they are under the control of the browser or the appletviewer. As with
GUI applications, packages java.awt and javax.swing should be imported into
applets. In pre-Swing applets (which would not have imported javax.swing , of
course), the programmer had to place the required drawing calls inside the inbuilt
Search WWH ::

Custom Search