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发行说明。
In general, you don't directly create a 通常,您不直接创建JRootPane
object. JRootPane
对象。Instead, you get a 但是,当您实例化JRootPane
(whether you want it or not!) when you instantiate JInternalFrame
or one of the top-level Swing containers, such as JApplet
, JDialog
, and JFrame
.JInternalFrame
或顶级Swing容器之一时(例如JApplet
、JDialog
和JFrame
)你会得到一个JRootPane
(不管你是否想要!)。
Using Top-Level Containers使用顶级容器 tells you the basics of using root panes getting the content pane, setting its layout manager, and adding Swing components to it. 告诉您使用根窗格的基本知识;获取内容窗格,设置其布局管理器,并向其中添加Swing组件。This section tells you more about root panes, including the components that make up a root pane and how you can use them.本节将介绍有关根窗格的更多信息,包括组成根窗格的组件以及如何使用它们。
As the preceding figure shows, a root pane has four parts:如上图所示,根窗格有四个部分:
paintComponent
method so that it does something, and it can intercept input events for the root pane. paintComponent
方法,以便它执行某些操作,并且它可以拦截根窗格的输入事件。setJMenuBar
method to put the menu bar in the appropriate place. setJMenuBar
方法将菜单栏放置在适当的位置。The glass pane is useful when you want to be able to catch events or paint over an area that already contains one or more components. 当您希望能够捕捉事件或在已包含一个或多个组件的区域上绘制时,玻璃窗格非常有用。For example, you can deactivate mouse events for a multi-component region by having the glass pane intercept the events. 例如,可以通过让玻璃窗格拦截事件来停用多组件区域的鼠标事件。Or you can display an image over multiple components using the glass pane.也可以使用玻璃窗格在多个组件上显示图像。
Here's a picture of an application that demonstrates glass pane features. 这是一个演示玻璃窗格功能的应用程序的图片。It contains a check box that lets you set whether the glass pane is "visible" whether it can get events and paint itself onscreen. 它包含一个复选框,用于设置玻璃窗格是否“可见”它是否可以获取事件并在屏幕上显示自己。When the glass pane is visible, it blocks all input events from reaching the components in the content pane. 当玻璃窗格可见时,它将阻止所有输入事件到达内容窗格中的组件。It also paints a red dot in the place where it last detected a mouse-pressed event.它还在上次检测到鼠标按下事件的位置绘制一个红点。
The following code from GlassPaneDemo.java
shows and hides the glass pane. GlassPaneDemo.java
中的以下代码显示和隐藏了玻璃窗格。This program happens to create its own glass pane. 这个程序碰巧创建了自己的玻璃窗格。However, if a glass pane doesn't do any painting, the program might simply attach listeners to the default glass pane, as returned by 但是,如果玻璃窗格不进行任何绘制,程序可能会简单地将侦听器附加到默认玻璃窗格,如getGlassPane
.getGlassPane
返回的。
myGlassPane = new MyGlassPane(...); changeButton.addItemListener(myGlassPane); frame.setGlassPane(myGlassPane); ... class MyGlassPane extends JComponent implements ItemListener { ... //React to change button clicks. public void itemStateChanged(ItemEvent e) { setVisible(e.getStateChange() == ItemEvent.SELECTED); } ... }
The next code snippet implements the mouse-event handling for the glass pane. 下一个代码片段实现了玻璃窗格的鼠标事件处理。If a mouse event occurs over the check box, then the glass pane redispatches the event so that the check box receives it.如果鼠标事件发生在复选框上,则玻璃窗格将重新修补该事件,以便复选框接收该事件。
...//In the implementation of the glass pane's mouse listener: public void mouseMoved(MouseEvent e) { redispatchMouseEvent(e, false); } .../* The mouseDragged, mouseClicked, mouseEntered, * mouseExited, and mousePressed methods have the same * implementation as mouseMoved. */... public void mouseReleased(MouseEvent e) { redispatchMouseEvent(e, true); } private void redispatchMouseEvent(MouseEvent e, boolean repaint) { Point glassPanePoint = e.getPoint(); Container container = contentPane; Point containerPoint = SwingUtilities.convertPoint( glassPane, glassPanePoint, contentPane); if (containerPoint.y < 0) { //we're not in the content pane //Could have special code to handle mouse events over //the menu bar or non-system window decorations, such as //the ones provided by the Java look and feel. } else { //The mouse event is probably over the content pane. //Find out exactly which component it's over. Component component = SwingUtilities.getDeepestComponentAt( container, containerPoint.x, containerPoint.y); if ((component != null) && (component.equals(liveButton))) { //Forward events over the check box. Point componentPoint = SwingUtilities.convertPoint( glassPane, glassPanePoint, component); component.dispatchEvent(new MouseEvent(component, e.getID(), e.getWhen(), e.getModifiers(), componentPoint.x, componentPoint.y, e.getClickCount(), e.isPopupTrigger())); } } //Update the glass pane if requested. if (repaint) { glassPane.setPoint(glassPanePoint); glassPane.repaint(); } }
Here is the code in 下面是MyGlassPane
that implements the painting.MyGlassPane
中实现绘画的代码。
protected void paintComponent(Graphics g) { if (point != null) { g.setColor(Color.red); g.fillOval(point.x - 10, point.y - 10, 20, 20); } }
A layered pane is a container with depth such that overlapping components can appear one on top of the other. 分层窗格是一个具有深度的容器,这样重叠的组件可以一个在另一个上出现。General information about layered panes is in How to Use Layered Panes. 有关分层窗格的一般信息,请参阅如何使用分层窗格。This section discusses the particulars of how root panes use layered panes.本节讨论根窗格如何使用分层窗格的详细信息。
Each root pane places its menu bar and content pane in an instance of 每个根窗格将其菜单栏和内容窗格放置在JLayeredPane
. JLayeredPane
的实例中。The Z ordering that the layered pane provides enables behavior such as displaying popup menus above other components.分层窗格提供的Z顺序启用了诸如在其他组件上方显示弹出菜单的行为。
You can choose to put components in the root pane's layered pane. 您可以选择将组件放在根窗格的分层窗格中。If you do, then you should be aware that certain depths are defined to be used for specific functions, and you should use the depths as intended. 如果您这样做了,那么您应该知道某些深度被定义为用于特定功能,并且您应该按照预期使用深度。Otherwise, your components might not play well with the others. 否则,您的组件可能无法与其他组件很好地配合。Here's a diagram that shows the functional layers and their relationship:下图显示了功能层及其关系:
The table below describes the intended use for each layer and lists the 下表描述了各层的预期用途,并列出了与各层相对应的JLayeredPane
constant that corresponds to each layer:JLayeredPane
常数:
FRAME_CONTENT_LAYER |
new Integer(-30000) |
|
DEFAULT_LAYER |
new Integer(0) |
|
PALETTE_LAYER |
new Integer(100) |
|
MODAL_LAYER |
new Integer(200) |
|
POPUP_LAYER |
new Integer(300) |
|
DRAG_LAYER |
new Integer(400) |
Here is a picture of RootLayeredPaneDemo, which is a version of LayeredPaneDemo that uses the root pane's layered pane, rather than creating a new layered pane.下面是RootLayeredPaneDemo的图片,它是LayeredPaneDemo版本,使用根窗格的分层窗格,而不是创建新的分层窗格。
The tables that follow list the API for using root panes, glass panes, and content panes. 下表列出了使用根窗格、玻璃窗格和内容窗格的API。For more information on using content panes, go to Using Top-Level Containers. 有关使用内容窗格的更多信息,请转到使用顶级容器。Here are the tables in this section:本节中的表格如下:
The API for using other parts of the root pane is described elsewhere:其他地方描述了使用根窗格其他部分的API:
JRootPane getRootPane() (in JApplet , JDialog , JFrame , JInternalFrame , and JWindow ) |
|
static JRootPane getRootPane(Component) (in SwingUtilities ) |
|
JRootPane getRootPane() (in JComponent ) |
SwingUtilities getRootPane method for the JComponent .JComponent 的SwingUtilities getRootPane 方法。 |
void setDefaultButton(JButton) JButton getDefaultButton() |
void setGlassPane(Component) Component getGlassPane() |
|
void setLayeredPane(JLayeredPane) Container getLayeredPane() |
|
void setContentPane(Container) Container getContentPane() |
|
void setJMenuBar(JMenuBar) JMenuBar getJMenuBar() (not defined in JWindow ) |
Every Swing program has a root pane, but few reference it directly. 每个Swing程序都有一个根窗格,但很少直接引用它。The examples in the following list illustrate how to use features of 以下列表中的示例说明了如何使用JRootPane
or the glass pane. JRootPane
或玻璃窗格的功能。Also see these lists:另请参阅以下列表:
GlassPaneDemo |
||
RootLayeredPaneDemo |
||
ListDialog |
JDialog .JDialog 的默认按钮。 | |
FrameDemo2 |
JFrame .JFrame 的默认按钮。 |