Documentation

The Java™ Tutorials
Hide TOC
Using Top-Level Containers使用顶级容器
Trail: Creating a GUI With Swing
Lesson: Using Swing Components

Using Top-Level Containers使用顶级容器

As we mentioned before, Swing provides three generally useful top-level container classes: JFrame, JDialog, and JApplet. 正如我们前面提到的,Swing提供了三个通常有用的顶级容器类:JFrameJDialogJAppletWhen using these classes, you should keep these facts in mind:在使用这些类时,您应该记住以下事实:


Note: Although JInternalFrame mimics JFrame, internal frames aren't actually top-level containers. 尽管JInternalFrame模仿JFrame,但内部框架实际上不是顶级容器。

Here's a picture of a frame created by an application. 这是一个应用程序创建的帧的图片。The frame contains a green menu bar (with no menus) and, in the frame's content pane, a large blank, yellow label.框架包含一个绿色菜单栏(没有菜单),并且在框架的内容窗格中有一个大的空白黄色标签。

A simple application with a frame that contains a menu bar and a content pane.
A diagram of the frame's major parts

You can find the entire source for this example in TopLevelDemo.java. 您可以在TopLevelDemo.java中找到该示例的完整源代码。Although the example uses a JFrame in a standalone application, the same concepts apply to JApplets and JDialogs.尽管该示例在独立应用程序中使用了JFrame,但相同的概念适用于JAppletsJDialogs

Here's the containment hierarchy for this example's GUI:下面是本示例GUI的包含层次结构:

Containment hierarchy for the TopLeveDemo example's GUI.

As the ellipses imply, we left some details out of this diagram. 正如省略号所暗示的,我们在该图中省略了一些细节。We reveal the missing details a bit later. 我们稍后会透露丢失的细节。Here are the topics this section discusses:以下是本节讨论的主题:

Top-Level Containers and Containment Hierarchies顶级容器和容器层次结构

Each program that uses Swing components has at least one top-level container. 使用Swing组件的每个程序至少有一个顶级容器。This top-level container is the root of a containment hierarchy — the hierarchy that contains all of the Swing components that appear inside the top-level container.该顶级容器是包含层次结构的根;包含顶级容器中显示的所有Swing组件的层次结构。

As a rule, a standalone application with a Swing-based GUI has at least one containment hierarchy with a JFrame as its root. 通常,具有基于Swing的GUI的独立应用程序至少有一个包含层次结构,其根是JFrameFor example, if an application has one main window and two dialogs, then the application has three containment hierarchies, and thus three top-level containers. 例如,如果一个应用程序有一个主窗口和两个对话框,那么该应用程序有三个包含层次结构,因此有三个顶级容器。One containment hierarchy has a JFrame as its root, and each of the other two has a JDialog object as its root.一个包含层次结构有一个JFrame作为其根,另外两个层次结构都有一个JDialog对象作为其根。

A Swing-based applet has at least one containment hierarchy, exactly one of which is rooted by a JApplet object. 基于Swing的小程序至少有一个包含层次结构,其中一个层次结构的根是JApplet对象。For example, an applet that brings up a dialog has two containment hierarchies. 例如,打开对话框的小程序有两个包含层次结构。The components in the browser window are in a containment hierarchy rooted by a JApplet object. 浏览器窗口中的组件位于以JApplet对象为根的包含层次结构中。The dialog has a containment hierarchy rooted by a JDialog object.该对话框有一个以JDialog对象为根的包含层次结构。

Adding Components to the Content Pane将组件添加到内容窗格

Here's the code that the preceding example uses to get a frame's content pane and add the yellow label to it:下面是上一个示例用于获取框架的内容窗格并向其添加黄色标签的代码:

frame.getContentPane().add(yellowLabel, BorderLayout.CENTER);

As the code shows, you find the content pane of a top-level container by calling the getContentPane method. 如代码所示,通过调用getContentPane方法,可以找到顶级容器的内容窗格。The default content pane is a simple intermediate container that inherits from JComponent, and that uses a BorderLayout as its layout manager.默认的内容窗格是一个简单的中间容器,它继承自JComponent,并使用BorderLayout作为其布局管理器。

It's easy to customize the content pane — setting the layout manager or adding a border, for example. 很容易定制内容窗格—例如,设置布局管理器或添加边框。However, there is one tiny gotcha. 然而,有一个小问题。The getContentPane method returns a Container object, not a JComponent object. getContentPane方法返回Container对象,而不是JComponent对象。This means that if you want to take advantage of the content pane's JComponent features, you need to either typecast the return value or create your own component to be the content pane. 这意味着,如果您想利用内容窗格的JComponent特性,您需要对返回值进行类型转换,或者创建自己的组件作为内容窗格。Our examples generally take the second approach, since it's a little cleaner. 示例通常采用第二种方法,因为它更干净。Another approach we sometimes take is to simply add a customized component to the content pane, covering the content pane completely.我们有时采取的另一种方法是简单地将自定义组件添加到内容窗格,完全覆盖内容窗格。

Note that the default layout manager for JPanel is FlowLayout; you'll probably want to change it.请注意,JPanel的默认布局管理器是FlowLayout;你可能想改变它。

To make a component the content pane, use the top-level container's setContentPane method. 要使组件成为内容窗格,请使用顶级容器的setContentPane方法。For example:例如:

//Create a panel and add components to it.
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(someBorder);
contentPane.add(someComponent, BorderLayout.CENTER);
contentPane.add(anotherComponent, BorderLayout.PAGE_END);


topLevelContainer.setContentPane(contentPane);

Note: 

As a convenience, the add method and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. 为了方便起见,add方法及其变体removesetLayout已被重写,以便根据需要转发到contentPaneThis means you can write这意味着你可以写作

frame.add(child);

and the child will be added to the contentPane.并且子项将被添加到contentPane

Note that only these three methods do this. 请注意,只有这三种方法可以做到这一点。This means that getLayout() will not return the layout set with setLayout().这意味着getLayout()将不会返回带有setLayout()的布局集。


Adding a Menu Bar添加菜单栏

In theory, all top-level containers can hold a menu bar. 理论上,所有顶级容器都可以容纳一个菜单栏。In practice, however, menu bars usually appear only in frames and applets. 然而,实际上,菜单栏通常只出现在框架和小程序中。To add a menu bar to a top-level container, create a JMenuBar object, populate it with menus, and then call setJMenuBar. 要将菜单栏添加到顶级容器,请创建JMenuBar对象,用菜单填充它,然后调用setJMenuBarThe TopLevelDemo adds a menu bar to its frame with this code:TopLevelDemo在其框架中添加了一个菜单栏,代码如下:

frame.setJMenuBar(greenMenuBar);

For more information about implementing menus and menu bars, see How to Use Menus.有关实现菜单和菜单栏的详细信息,请参阅如何使用菜单

The Root Pane根窗格

Each top-level container relies on a reclusive intermediate container called the root pane. 每个顶级容器都依赖于一个名为根窗格的封闭中间容器。The root pane manages the content pane and the menu bar, along with a couple of other containers. 根窗格管理内容窗格和菜单栏,以及一些其他容器。You generally don't need to know about root panes to use Swing components. 使用Swing组件通常不需要了解根窗格。However, if you ever need to intercept mouse clicks or paint over multiple components, you should get acquainted with root panes.但是,如果您需要拦截鼠标单击或在多个组件上绘制,您应该熟悉根窗格。

Here's a list of the components that a root pane provides to a frame (and to every other top-level container):下面是根窗格提供给框架(以及每个其他顶级容器)的组件列表:

A root pane manages four other panes: a layered pane, a menu bar, a content pane, and a glass pane.

We've already told you about the content pane and the optional menu bar. 我们已经向您介绍了内容窗格和可选菜单栏。The two other components that a root pane adds are a layered pane and a glass pane. 根窗格添加的另外两个组件是分层窗格和玻璃窗格。The layered pane contains the menu bar and content pane, and enables Z-ordering of other components. 分层窗格包含菜单栏和内容窗格,并支持其他组件的Z排序。The glass pane is often used to intercept input events occuring over the top-level container, and can also be used to paint over multiple components.玻璃窗格通常用于拦截顶级容器上发生的输入事件,也可用于绘制多个组件。

For more details, see How to Use Root Panes.有关详细信息,请参阅如何使用根窗格


Previous page: Using Swing Components
Next page: The JComponent Class