Documentation

The Java™ Tutorials
Hide TOC
Getting Started With Applets小程序入门
Trail: Deployment
Lesson: Java AppletsJava小程序

Getting Started With Applets小程序入门

The HelloWorld applet shown next is a Java class that displays the string "Hello World".接下来显示的HelloWorld小程序是一个显示字符串“HelloWorld”的Java类。


Note:  If you don't see the example running, you might need to enable the JavaScript interpreter in your browser so that the Deployment Toolkit script can function properly.如果没有看到示例正在运行,则可能需要在浏览器中启用JavaScript解释器,以便部署工具包脚本能够正常工作。

Following is the source code for the HelloWorld applet:以下是HelloWorld小程序的源代码:

import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;

public class HelloWorld extends JApplet {
    //Called when this applet is loaded into the browser.
    public void init() {
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    JLabel lbl = new JLabel("Hello World");
                    add(lbl);
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }
}

An applet such as this is typically managed and run by the Java Plug-in software in the browser.这样的小程序通常由浏览器中的Java插件软件管理和运行。

Download source code for the Hello World example to experiment further.下载Hello World示例的源代码以进行进一步实验。


Previous page: Java Applets
Next page: Defining an Applet Subclass