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发行说明。
An applet can react to major events in the following ways:小程序可以通过以下方式对重大事件做出反应:
This section introduces a new applet, 本节介绍一个新的applet Simple
, that uses all of these methods. Simple
,它使用了所有这些方法。Unlike Java applications, applets do not need to implement a 与Java应用程序不同,小程序不需要实现main
method.main
方法。
Here is the 下面是一个Simple
applet.Simple
小程序。
The following is the source code for the 下面是这个Simple
applet. Simple
小程序的源代码。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. Applet
类被扩展,而不是Swing JApplet
类,因为Swing组件不需要添加到此小程序中。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.Applet
子类)的实例。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 initializes 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 code for the Simple Applet example to experiment further.下载Simple
小程序示例的源代码以进行进一步实验。