Java 教程是为 JDK 8 编写的。本页中描述的示例和实践未利用在后续版本中引入的改进。
The
Applet
class provides a framework for applet execution, defining methods that the system calls when milestones occur. Milestones are major events in an applet's life cycle. Most applets override some or all of these methods to respond appropriately to milestones.
init
MethodThe init
method is useful for one-time initialization that doesn't take very long. The init
method typically contains the code that you would normally put into a constructor. The reason applets don't usually have constructors is that they aren't guaranteed to have a full environment until their init
method is called. Keep the init
method short so that your applet can load quickly.
start
MethodEvery applet that performs tasks after initialization (except in direct response to user actions) must override the start
method. The start
method starts the execution of the applet. It is good practice to return quickly from the start
method. If you need to perform computationally intensive operations it might be better to start a new thread for this purpose.
stop
MethodMost applets that override the start
should also override the stop
method. The stop
method should suspend the applet's execution, so that it doesn't take up system resources when the user isn't viewing the applet's page. For example, an applet that displays an animation should stop trying to draw the animation when the user isn't viewing it.
destroy
MethodMany applets don't need to override the destroy
method because their stop
method (which is called before destroy
) will perform all tasks necessary to shut down the applet's execution. However, the destroy
method is available for applets that need to release additional resources.
destroy
method as short as possible, because there is no guarantee that this method will be completely executed. The Java Virtual Machine might exit before a long destroy
method has completed.