Documentation

The Java™ Tutorials
Hide TOC
Methods for Milestones里程碑的方法
Trail: Deployment
Lesson: Java Applets
Section: Getting Started With Applets

Methods for Milestones里程碑的方法

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 Method方法

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 Method方法

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 Method方法

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 Method方法

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方法适用于需要释放额外资源的小程序。


Note: Keep implementations of the destroy method as short as possible, because there is no guarantee that this method will be completely executed. 保持destroy方法的实现尽可能短,因为无法保证该方法将被完全执行。The Java Virtual Machine might exit before a long destroy method has completed. Java虚拟机可能在长时间destroy方法完成之前退出。

Previous page: Defining an Applet Subclass
Next page: Life Cycle of an Applet