Java Reference
In-Depth Information
applet. When the master page is loaded, the browser fetches the code of the applet
(its class file) and starts the Java runtime system to execute the code.
Applets are driven by four methods, which we explain below. Even though
applets can have a constructor, it should not be used.
public void init()
public void start()
public void stop()
public void destroy()
init() is automatically called by the browser when the master page is loaded
into the browser for the first time after the browser has started. This method
replaces the constructor. All the code for embedding other components initial-
izing variables, etc., should go here. This method is only executed once in the
applet's life.
start() is automatically called by the browser after the init has finished. It is
also called when the user returns to the master page after having looked at other
pages without shutting the browser down. The code for resuming interrupted
work should go here.
stop() is automatically called by the browser when the user leaves the master
page without shutting the browser down. As the applet is paused, all code for
interrupting the computations should go here. Some browsers also call destroy
at this point. Then the applet is terminated. It is restarted using init when the
user returns to this page.
destroy() is automatically called by the browser just before the applet is termi-
nated. All code for the final clean-up should go here.
In our examples we only put real code into the init method. The other three
methods contain only print commands. The result of these commands will appear
on the Java console . The Java console is a program that comes with the Java plug-
ins for browsers. How it is activated depends on the browser and the operating
system. In recent versions of Windows one can find it in Start -> Settings ->
Java Plug-in .
23.1.1
A counter applet
In our first example we reuse the CounterPanel from Chapter 3. Such a panel is
glued centrally into the applet. No further code is needed, because a CounterPanel
supplies all functions of a counter. We list the program below.
File: its/Applet/CounterApplet.java
1. package its.Applet;
2.
3. import javax.swing.JApplet;
Search WWH ::




Custom Search