Java Reference
In-Depth Information
Java applets are Java programs running from a Web browser.
Key
Point
When browsing the Web, often the graphical user interface and animation you see have been
developed using Java. The Java programs that run from a Web browser are called
Java applets
.
How do you write Java applets with graphics, images, and audio? This chapter will show you how.
Java applets are instances of the
Applet
class.
JApplet
is a subclass of
Applet
,
and it is suitable for developing applets using Swing components.
Key
Point
So far, you have used and written Java applications. Everything you have learned about writ-
ing applications, however, applies also to writing applets. Applications and applets share
many common programming features, although they differ slightly in some aspects. For
example, every application must have a
main
method, which is invoked by the Java inter-
preter. Java applets, on the other hand, do not need a
main
method. They run in the Web
browser environment. Because applets are embedded in a Web page, Java provides special
features that enable applets to run from a Web browser.
The
Applet
class provides the essential framework that enables applets to be run from a
Web browser. While every Java application has a
main
method that is executed when the
application starts, applets, because they don't have a
main
method, depend on the browser to
run them. Every applet is an instance of
java.applet.Applet
. The
Applet
class is an
AWT class and is not designed to work with Swing components. To use Swing components in
Java applets, you need to define a Java applet that extends
javax.swing.JApplet
, which is
a subclass of
java.applet.Applet
.
Every Java GUI program you have developed can be converted into an applet by replacing
JFrame
with
JApplet
and deleting the
main
method. Figure 18.1a shows a Java GUI appli-
cation program, which can be converted into a Java applet as shown in Figure 18.1b.
VideoNote
First applet
import
javax.swing.*;
import
javax.swing.*;
JApplet
public class
DisplayLabel
extends
JFrame {
public
DisplayLabel() {
add(
new
JLabel(
"Great!"
, JLabel.CENTER));
public class
DisplayLabel
extends
JFrame {
public
DisplayLabel() {
add(
new
JLabel(
"Great!"
, JLabel.CENTER));
}
}
public static void
main(String[] args) {
JFrame frame =
new
DisplayLabel();
frame.setTitle(
"DisplayLabel"
);
frame.setSize(
200
,
100
);
frame.setLocationRelativeTo(
null
);
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.setVisible(
true
);
public static void
main(String[] args) {
JFrame frame =
new
DisplayLabel();
frame.setTitle(
"DisplayLabel"
);
frame.setSize(
200
,
100
);
frame.setLocationRelativeTo(
null
);
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.setVisible(
true
);
}
}
}
}
(a) GUI application
(b) Applet
F
IGURE
18.1
You can convert a GUI application into an applet.
Listing 18.1 gives the complete code for the applet.
L
ISTING
18.1
DisplayLabel.java
1
import
javax.swing.*;
2
3
public class
DisplayLabel
extends
JApplet
{
extend
Japplet