The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The Applet
class provides a framework for applet execution, defining methods that the system calls when milestones occur. Applet
类为Applet执行提供了一个框架,定义了里程碑发生时系统调用的方法。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
The init
method is useful for one-time initialization that doesn't take very long. init
方法对于一次性初始化非常有用,不会花费很长时间。The init
method typically contains the code that you would normally put into a constructor. init
方法通常包含通常放入构造函数的代码。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. init
方法之前,它们不能保证拥有完整的环境。Keep the 保持init
method short so that your applet can load quickly.init
方法简短,以便小程序可以快速加载。
start
Every applet that performs tasks after initialization (except in direct response to user actions) must override the 初始化后执行任务的每个小程序(直接响应用户操作的小程序除外)都必须覆盖start
method. start
方法。The start
method starts the execution of the applet. start
方法启动小程序的执行。It is good practice to return quickly from the 从start
method. start
方法快速返回是一种很好的做法。If you need to perform computationally intensive operations it might be better to start a new thread for this purpose.如果需要执行计算密集型操作,最好为此启动一个新线程。
stop
Most applets that override the 大多数覆盖start
should also override the stop
method. start
的小程序也应该覆盖stop
方法。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. stop
方法应该暂停小程序的执行,这样当用户不查看小程序的页面时,它就不会占用系统资源。For example, an applet that displays an animation should stop trying to draw the animation when the user isn't viewing it.例如,当用户不查看动画时,显示动画的小程序应该停止尝试绘制动画。
destroy
Many 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. destroy
方法,因为它们的stop
方法(在destroy
之前调用)将执行关闭小程序执行所需的所有任务。However, the 但是,destroy
method is available for applets that need to release additional resources.destroy
方法适用于需要释放额外资源的小程序。
destroy
method as short as possible, because there is no guarantee that this method will be completely executed. destroy
方法的实现尽可能短,因为无法保证该方法将被完全执行。destroy
method has completed. destroy
方法完成之前退出。