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 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:
MyTopJPanel
that is a subclass of javax.swing.JPanel
. MyTopJPanel
类,它是javax.swing.JPanel
的子类。MyTopJPanel
class.MyTopJPanel
类的构造函数中布置小程序的GUI组件。MyApplet
that is a subclass of javax.swing.JApplet
.MyApplet
的类,它是javax.swing.JApplet
的子类。init
method of MyApplet
, instantiate MyTopJPanel
and set it as the applet's content pane.MyApplet
的init
方法中,实例化MyTopJPanel
并将其设置为小程序的内容窗格。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组件的更多信息。
JPanel
ClassJPanel
类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
类是最顶层的JPanel
。The 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); } // ... }
For a Java applet that has a Swing-based GUI, create a class that is a subclass of 对于具有基于Swing的GUI的Java小程序,创建一个类,该类是javax.swing.JApplet
. javax.swing.JApplet
的子类。An applet that does not contain a Swing-based GUI can extend the 不包含基于Swing的GUI的小程序可以扩展java.applet.Applet
class.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); } }
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 在Dynamic Tree Demo示例中,核心功能位于DynamicTreePanel
class. 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.因此,为了保持可移植性并保持部署选项的开放性,请遵循本页所述的基于组件的设计。