Java 教程是为 JDK 8 编写的。本页中描述的示例和实践未利用在后续版本中引入的改进。
An applet can react to major events in the following ways:
This section introduces a new applet, Simple
, that uses all of these methods. Unlike Java applications, applets do not need to implement a main
method.
Here is the Simple
applet.
The following is the source code for the Simple
applet. This applet displays a descriptive string whenever it encounters a major milestone in its life, such as when the user first visits the page the applet is on.
import java.applet.Applet; import java.awt.Graphics; //No need to extend JApplet, since we don't add any components; //we just paint. public class Simple extends Applet { StringBuffer buffer; public void init() { buffer = new StringBuffer(); addItem("initializing... "); } public void start() { addItem("starting... "); } public void stop() { addItem("stopping... "); } public void destroy() { addItem("preparing for unloading..."); } private void addItem(String newWord) { System.out.println(newWord); buffer.append(newWord); repaint(); } public void paint(Graphics g) { //Draw a Rectangle around the applet's display area. g.drawRect(0, 0, getWidth() - 1, getHeight() - 1); //Draw the current string inside the rectangle. g.drawString(buffer.toString(), 5, 15); } }
Applet
class is extended, not the Swing JApplet
class, as Swing components do not need to be added to this applet.
As a result of the applet being loaded, you should see the text "initializing... starting...". When an applet is loaded, here's what happens:
Applet
subclass) is created.When the user leaves the page, for example, to go to another page, the browser stops and destroys the applet. The state of the applet is not preserved. When the user returns to the page, the browser intializes and starts a new instance of the applet.
When you refresh or reload a browser page, the current instance of the applet is stopped and destroyed and a new instance is created.
When the user quits the browser, the applet has the opportunity to stop itself and perform a final cleanup before the browser exits.
Download source codefor the Simple Applet example to experiment further.