双语版Java程序设计
上QQ阅读APP看本书,新人免费读10天
设备和账号都新为新人

1.5 Java Applet

A Java applet is an applet delivered to users in the form of Java bytecode. Java applets can run in a Web browser using a Java Virtual Machine (JVM), or in Sun's AppletViewer, a stand-alone tool for testing applets. Java applets were introduced in the first version of the Java language in 1995, and are written in programming languages that compile to Java bytecode, usually in Java, but also in other languages such as Jython, JRuby, or Eiffel (via SmartEiffel).

Java applets run at speeds comparable to, but generally slower than, other compiled languages such as C++, but until approximately 2011 many times faster than JavaScript. In addition they can use 3D hardware acceleration that is available from Java. This makes applets well suited for non trivial, computation intensive visualizations. When browsers have gained support for native hardware accelerated graphics in the form of Canvas and WebGL, as well as Just In Time compiled JavaScript, the speed difference has become less noticeable.

Applet或Java小应用程序是一种在Web环境下,运行于客户端的Java程序组件。它也是20世纪90年代中期,Java在诞生后得以一炮走红的功臣之一。通常,每个Applet的功能都比较单一(如仅用于显示一个舞动的Logo),因此它被称做“小应用程序”。

Since Java's bytecode is cross-platform or platform independent, Java applets can be executed by browsers for many platforms, including Microsoft Windows, Unix, Mac OS and Linux. It is also trivial to run a Java applet as an application with very little extra code. This has the advantage of running a Java applet in offline mode without the need for any Internet browser software and also directly from the integrated development environment (IDE).

The applet can be displayed on the web page by making use of the deprecated applet HTML element, or the recommended object element. Embed element can be used with Mozilla family browsers (embed is no longer deprecated in since HTML 5). This specifies the applet's source and location. Object and embed tags can also download and install Java virtual machine (if required) or at least lead to the plug-in page. Applet and object tags also support loading of the serialized applets that start in some particular (rather than initial) state. Tags also specify the message that shows up in place of the applet if the browser cannot run it due any reason.

Applet必须运行于某个特定的“容器”,这个容器可以是浏览器本身,也可以是通过各种插件,或者包括支持Applet的移动设备在内的其他各种程序来运行。

However, despite object being officially a recommended tag, as of 2010, the support of the object tag was not yet consistent among browsers and Sun kept recommending the older applet tag for deploying in multibrowser environments, as it remained the only tag consistently supported by the most popular browsers. To support multiple browsers, the object tag currently requires JavaScript (that recognizes the browser and adjusts the tag), usage of additional browser-specific tags or delivering adapted output from the server side. Deprecating applet tag has been criticized. Oracle now provides a maintained JavaScript code to launch applets with cross platform workarounds

与一般的Java应用程序不同,Applet不是通过main方法来运行的(参见Java的Hello World程序和Java Applet的Hello World程序)。在运行时Applet通常会与用户进行互动,显示动态的画面,并且会遵循严格的安全检查,阻止潜在的不安全因素(如根据安全策略,限制Applet对客户端文件系统的访问)。