Documentation

The Java™ Tutorials
Hide TOC
Developing an Applet开发Applet
Trail: Deployment
Lesson: Java Applets
Section: Getting Started With Applets小程序入门

Developing an Applet开发Applet

An application designed using component-based architecture can be developed into a Java applet. 使用基于组件的体系结构设计的应用程序可以开发成Java小程序。Consider the example of a Java applet with a Swing-based graphical user interface (GUI). 请考虑使用基于Swing的图形用户界面(GUI)的java applet的示例。With component-based design, the GUI can be built with smaller building blocks or components. 通过基于组件的设计,GUI可以用更小的构建块或组件构建。The following general steps are used to create an applet GUI:以下一般步骤用于创建小程序GUI:

The following sections explore these steps in greater detail by using the Dynamic Tree Demo applet. 以下各节将使用动态树演示小程序更详细地探讨这些步骤。If you are not familiar with Swing, see Creating a GUI with Swing to learn more about using Swing GUI components.如果您不熟悉Swing,请参阅使用Swing创建GUI,以了解有关使用Swing GUI组件的更多信息。


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解释器,以便部署工具包脚本能够正常运行。

Creating the Top JPanel Class创建顶级JPanel

Create a class that is a subclass of JPanel. 创建一个JPanel的子类。This top JPanel acts as a container for all your other UI components. 这个顶部的JPanel充当所有其他UI组件的容器。In the following example, the DynamicTreePanel class is the topmost JPanel. 在下面的示例中,DynamicTreePanel类是最顶层的JPanelThe constructor of the DynamicTreePanel class invokes other methods to create and lay out the UI controls properly.DynamicTreePanel类的构造函数调用其他方法来正确地创建和布局UI控件。

public class DynamicTreePanel extends JPanel implements ActionListener {
    private int newNodeSuffix = 1;
    private static String ADD_COMMAND = "add";
    private static String REMOVE_COMMAND = "remove";
    private static String CLEAR_COMMAND = "clear";
    
    private DynamicTree treePanel;

    public DynamicTreePanel() {
        super(new BorderLayout());
        
        //Create the components.
        treePanel = new DynamicTree();
        populateTree(treePanel);

        JButton addButton = new JButton("Add");
        addButton.setActionCommand(ADD_COMMAND);
        addButton.addActionListener(this);
        
        JButton removeButton = new JButton("Remove");
        
        // ...
        
        JButton clearButton = new JButton("Clear");
        
        // ...
        
        //Lay everything out.
        treePanel.setPreferredSize(
            new Dimension(300, 150));
        add(treePanel, BorderLayout.CENTER);

        JPanel panel = new JPanel(new GridLayout(0,3));
        panel.add(addButton);
        panel.add(removeButton); 
        panel.add(clearButton);
        add(panel, BorderLayout.SOUTH);
    }
    // ...
}

Creating the Applet创建小程序

For a Java applet that has a Swing-based GUI, create a class that is a subclass of javax.swing.JApplet. 对于具有基于Swing的GUI的Java小程序,创建一个类,该类是javax.swing.JApplet的子类。An applet that does not contain a Swing-based GUI can extend the java.applet.Applet class.不包含基于Swing的GUI的小程序可以扩展java.applet.Applet类。

Override the applet's init method to instantiate your top JPanel class and create the applet's GUI. 覆盖小程序的init方法来实例化您的顶级JPanel类并创建小程序的GUI。The init method of the DynamicTreeApplet class invokes the createGUI method in the AWT Event Dispatcher thread.DynamicTreeApplet类的init方法调用AWT Event Dispatcher线程中的createGUI方法。

package appletComponentArch;

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

public class DynamicTreeApplet 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() {
                    createGUI();
                }
            });
        } catch (Exception e) { 
            System.err.println("createGUI didn't complete successfully");
        }
    }
    
    private void createGUI() {
        //Create and set up the content pane.
        DynamicTreePanel newContentPane = new DynamicTreePanel();
        newContentPane.setOpaque(true); 
        setContentPane(newContentPane);        
    }        
}

Benefits of Separating Core Functionality From the Final Deployment Mechanism将核心功能与最终部署机制分离的好处

Another way to create an applet is to just remove the layer of abstraction (separate top JPanel) and lay out all the controls in the applet's init method itself. 创建小程序的另一种方法是删除抽象层(单独的顶部JPanel),并在小程序的init方法本身中布局所有控件。The downside to creating the GUI directly in the applet is that it will now be more difficult to deploy your functionality as a Java Web Start application, if you choose to do so later.直接在小程序中创建GUI的缺点是,如果以后选择将功能部署为Java Web Start应用程序,那么现在将更加困难。

In the Dynamic Tree Demo example, the core functionality resides in the DynamicTreePanel class. 在Dynamic Tree Demo示例中,核心功能位于DynamicTreePanel类中。It is now trivial to drop the DynamicTreePanel class into a JFrame and deploy as a Java Web Start application.现在,将DynamicTreePanel类放入JFrame并部署为JavaWebStart应用程序是很简单的。

Hence, to preserve portability and keep deployment options open, follow component-based design as described on this page.因此,为了保持可移植性并保持部署选项的开放性,请遵循本页所述的基于组件的设计。

Download source code for the Dynamic Tree Demo Applet example to experiment further.下载动态树演示Applet示例的源代码以进行进一步的实验。

Previous page: Applet's Execution Environment
Next page: Deploying an Applet