Java Reference
In-Depth Information
18.6.1 The
init
Method
The
init
method is invoked after the applet is created. If a subclass of
Applet
has an ini-
tialization to perform, it should override this method. The functions usually implemented in
this method include getting string parameter values from the
<applet>
tag in the HTML
page.
init()
18.6.2 The
start
Method
The
start
method is invoked after the
init
method. It is also called when the user returns to
the Web page containing the applet after surfing other pages.
A subclass of
Applet
overrides this method if it has any operation that needs to be per-
formed whenever the Web page containing the applet is visited. An applet with animation, for
example, might start the timer to resume animation.
start()
18.6.3 The
stop
Method
The
stop
method is the opposite of the
start
method. The
start
method is called when the
user moves back to the page that contains the applet. The
stop
method is invoked when the
user leaves the page.
A subclass of
Applet
overrides this method if it has any operation to be performed each
time the Web page containing the applet is no longer visible. An applet with animation, for
example, might stop the timer to pause animation.
stop()
18.6.4 The
destroy
Method
The
destroy
method is invoked when the browser exits normally to inform the applet that it
is no longer needed and should release any resources it has allocated. The
stop
method is
always called before the
destroy
method.
A subclass of
Applet
overrides this method if it has any operation to be performed before
it is destroyed. Usually, you won't need to override this method unless you want to release
specific resources that the applet created.
destroy()
✓
✓
18.10
Describe the
init()
,
start()
,
stop()
, and
destroy()
methods in the
Applet
class.
Check
Point
18.11
Why does the applet in (a) below display nothing? Why does the applet in (b) have
a runtime
NullPointerException
on the highlighted line?
import
javax.swing.*;
import
javax.swing.*;
public class
WelcomeApplet
extends
JApplet {
public void
WelcomeApplet() {
JLabel jlblMessage =
new
JLabel(
"It is Java"
);
public class
WelcomeApplet
extends
JApplet {
private
JLabel jlblMessage;
public
WelcomeApplet() {
JLabel jlblMessage =
new
JLabel(
"It is Java"
);
}
}
}
@Override
public void
init() {
add(jlblMessage);
}
}
(a)
(b)