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发行说明。
As we mentioned before, Swing provides three generally useful top-level container classes: 正如我们前面提到的,Swing提供了三个通常有用的顶级容器类:JFrame
, JDialog
, and JApplet
. JFrame
、JDialog
和JApplet
。When using these classes, you should keep these facts in mind:在使用这些类时,您应该记住以下事实:
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.框架包含一个绿色菜单栏(没有菜单),并且在框架的内容窗格中有一个大的空白黄色标签。
![]() |
![]() |
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 JApplet
s and JDialog
s.JFrame
,但相同的概念适用于JApplets
和JDialogs
。
Here's the containment hierarchy for this example's GUI:下面是本示例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:以下是本节讨论的主题:
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 通常,具有基于Swing的GUI的独立应用程序至少有一个包含层次结构,其根是JFrame
as its root. JFrame
。For 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 基于Swing的小程序至少有一个包含层次结构,其中一个层次结构的根是JApplet
object. 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
对象为根的包含层次结构。
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);
As a convenience, the 为了方便起见,add
method and its variants, remove
and setLayout
have been overridden to forward to the contentPane
as necessary. add
方法及其变体remove
和setLayout
已被重写,以便根据需要转发到contentPane
。This 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()
的布局集。
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
对象,用菜单填充它,然后调用setJMenuBar
。The 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.有关实现菜单和菜单栏的详细信息,请参阅如何使用菜单。
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):下面是根窗格提供给框架(以及每个其他顶级容器)的组件列表:
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.有关详细信息,请参阅如何使用根窗格。