Documentation

The Java™ Tutorials
Hide TOC
How to Use Internal Frames如何使用内部框架
Trail: Creating a GUI With Swing
Lesson: Using Swing Components
Section: How to Use Various Components

How to Use Internal Frames如何使用内部框架

With the JInternalFrame class you can display a JFrame-like window within another window. 使用JInternalFrame类,您可以在另一个窗口中显示类似JFrame的窗口。Usually, you add internal frames to a desktop pane. 通常,将内部框架添加到桌面窗格。The desktop pane, in turn, might be used as the content pane of a JFrame. 反过来,桌面窗格可以用作JFrame的内容窗格。The desktop pane is an instance of JDesktopPane, which is a subclass of JLayeredPane that has added API for managing multiple overlapping internal frames.桌面窗格是JDesktopPane的一个实例,它是JLayeredPane的子类,添加了用于管理多个重叠内部帧的API。

You should consider carefully whether to base your program's GUI around frames or internal frames. Switching from internal frames to frames or vice versa is not necessarily a simple task. 您应该仔细考虑程序的GUI是基于框架还是基于内部框架。从内部帧切换到帧或从内部帧转换到帧不一定是一项简单的任务。By experimenting with both frames and internal frames, you can get an idea of the tradeoffs involved in choosing one over the other.通过对框架和内部框架进行实验,您可以了解选择一个框架而不是另一个框架所涉及的权衡。

Here is a picture of an application that has two internal frames (one of which is iconified) inside a regular frame:以下是一个应用程序的图片,该应用程序在一个常规框架内有两个内部框架(其中一个是图标化的):

InternalFrameDemo has multiple internal frames, managed by a desktop pane

Try this: 
  1. Click the Launch button to run InternalFrameDemo using Java™ Web Start (download JDK 7 or later). 单击启动按钮,使用Java™Web启动运行InternalFrameDemo(下载JDK 7或更高版本)。Alternatively, to compile and run the example yourself, consult the example index.或者,要自己编译和运行示例,请参考示例索引Launches the InternalFrameDemo example
  2. Create new internal frames using the Create item in the Document menu.使用“文档”菜单中的“创建”项创建新的内部框架。
    Each internal frame comes up 30 pixels lower and to the right of the place where the previous internal frame first appeared. 每个内部帧在前一个内部帧首次出现的位置的下方和右侧出现30个像素。This functionality is implemented in the MyInternalFrame class, which is the custom subclass of JInternalFrame.此功能在MyInternalFrame类中实现,该类是JInternalFrame的自定义子类。

The following code, taken from InternalFrameDemo.java, creates the desktop and internal frames in the previous example.下面的代码取自InternalFrameDemo.java,在前面的示例中创建桌面和内部框架。

...//In the constructor of InternalFrameDemo, a JFrame subclass:
    desktop = new JDesktopPane();
    createFrame(); //Create first window
    setContentPane(desktop);
    ...
    //Make dragging a little faster but perhaps uglier.
    desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
...
protected void createFrame() {
    MyInternalFrame frame = new MyInternalFrame();
    frame.setVisible(true);
    desktop.add(frame);
    try {
        frame.setSelected(true);
    } catch (java.beans.PropertyVetoException e) {}
}

...//In the constructor of MyInternalFrame, a JInternalFrame subclass:
static int openFrameCount = 0;
static final int xOffset = 30, yOffset = 30;

public MyInternalFrame() {
    super("Document #" + (++openFrameCount),
          true, //resizable
          true, //closable
          true, //maximizable
          true);//iconifiable
    //...Create the GUI and put it in the window...
    //...Then set the window size or call pack...
    ...
    //Set the window's location.
    setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
}

Internal Frames vs. Regular Frames内部框架与常规框架

The code for using internal frames is similar in many ways to the code for using regular Swing frames. Because internal frames have root panes, setting up the GUI for a JInternalFrame is very similar to setting up the GUI for a JFrame. 使用内部框架的代码在许多方面与使用常规摆动框架的代码类似。由于内部框架具有根窗格,因此为JInternalFrame设置GUI与为JFrame设置图形用户界面非常相似。JInternalFrame also provides other API, such as pack, that makes it similar to JFrame.还提供了其他API,如pack,使其类似于JFrame


Note: 

Just as for a regular frame, you must invoke setVisible(true) or show() on an internal frame to display it. 与常规帧一样,您必须在内部帧上调用setVisible(true)show()来显示它。The internal frame does not appear until you explicitly make it visible.在明确显示内部框架之前,内部框架不会显示。


Internal frames are not windows or top-level containers, however, which makes them different from frames. 但是,内部框架不是窗口或顶级容器,这使它们与框架不同。For example, you must add an internal frame to a container (usually a JDesktopPane); an internal frame cannot be the root of a containment hierarchy. 例如,必须将内部框架添加到容器(通常是JDesktopPane);内部框架不能是包含层次结构的根。Also, internal frames do not generate window events. 此外,内部帧不生成窗口事件。Instead, the user actions that would cause a frame to fire window events cause an internal frame to fire internal frame events.相反,会导致帧触发窗口事件的用户操作会导致内部帧触发内部帧事件。

Because internal frames are implemented with platform-independent code, they add some features that frames cannot give you. 因为内部框架是用独立于平台的代码实现的,所以它们添加了一些框架无法提供的功能。One such feature is that internal frames give you more control over their state and capabilities than frames do. 一个这样的特性是,内部框架比框架更能控制它们的状态和功能。You can programmatically iconify or maximize an internal frame. 您可以以编程方式图标化或最大化内部框架。You can also specify what icon goes in the internal frame's title bar. 您还可以指定内部框架标题栏中的图标。You can even specify whether the internal frame has the window decorations to support resizing, iconifying, closing, and maximizing.您甚至可以指定内部框架是否具有支持调整大小、图标化、关闭和最大化的窗口装饰。

Another feature is that internal frames are designed to work within desktop panes. 另一个特点是,内部框架设计用于在桌面窗格中工作。The JInternalFrame API contains methods such as moveToFront that work only if the internal frame's container is a layered pane such as a JDesktopPane.JInternalFrame API包含moveToFront等方法,这些方法仅在内部框架的容器是分层窗格(如JDesktopPane)时才有效。

Rules of Using Internal Frames使用内部框架的规则

If you have built any programs using JFrame and the other Swing components, then you already know a lot about how to use internal frames. 如果您已经使用JFrame和其他Swing组件构建了任何程序,那么您已经知道了很多关于如何使用内部框架的知识。The following list summarizes the rules for using internal frames. 下表总结了使用内部框架的规则。For additional information, see How to Make Frames and The JComponent Class.有关其他信息,请参阅如何生成框架JComponent

You must set the size of the internal frame.必须设置内部框架的大小。
If you do not set the size of the internal frame, it will have zero size and thus never be visible. 如果不设置内部框架的大小,则其大小将为零,因此永远不可见。You can set the size using one of the following methods: setSize, pack, or setBounds.可以使用以下方法之一设置大小:setSizepacksetBounds
As a rule, you should set the location of the internal frame.通常,应设置内部框架的位置。
If you do not set the location of the internal frame, it will come up at 0,0 (the upper left of its container). 如果不设置内部框架的位置,它将出现在0,0(其容器的左上角)。You can use the setLocation or setBounds method to specify the upper left point of the internal frame, relative to its container.可以使用setLocationsetBounds方法指定内部框架相对于其容器的左上点。
To add components to an internal frame, you add them to the internal frame's content pane.要将组件添加到内部框架,请将其添加到内部框的内容窗格中。
This is exactly like the JFrame situation. 这与JFrame的情况完全相同。See Adding Components to the Content Pane for details.有关详细信息,请参阅将组件添加到内容窗格
Dialogs that are internal frames should be implemented using JOptionPane or JInternalFrame, not JDialog.作为内部框架的对话框应该使用JOptionPaneJInternalFrame实现,而不是JDialog
To create a simple dialog, you can use the JOptionPane showInternalXxxDialog methods, as described in How to Make Dialogs.要创建一个简单的对话框,可以使用JOptionPane showInternalXxxDialog方法,如如何创建对话框中所述。
You must add an internal frame to a container.必须向容器添加内部框架。
If you do not add the internal frame to a container (usually a JDesktopPane), the internal frame will not appear.如果不将内部框架添加到容器(通常是JDesktopPane),则不会显示内部框架。
You need to call show or setVisible on internal frames.您需要在内部框架上调用showsetVisible
Internal frames are invisible by default. 默认情况下,内部框架不可见。You must invoke setVisible(true) or show() to make them visible.必须调用setVisible(true)show()使它们可见。
Internal frames fire internal frame events, not window events.内部帧触发内部帧事件,而不是窗口事件。
Handling internal frame events is almost identical to handling window events. 处理内部帧事件与处理窗口事件几乎相同。See How to Write an Internal Frame Listener for more information.有关详细信息,请参阅如何编写内部帧侦听器


Performance tip:性能提示: 

When a desktop has many internal frames, the user might notice that moving them seems slow. 当桌面有许多内部框架时,用户可能会注意到移动它们似乎很慢。Outline dragging is one way to avoid this problem. 轮廓拖动是避免此问题的一种方法。With outline dragging, only the outline of the internal frame is painted at the current mouse position while the internal frame's being dragged. 通过轮廓拖动,在拖动内部框架时,仅在当前鼠标位置绘制内部框架的轮廓。The internal frame's innards are not repainted at a new position until dragging stops. 在拖动停止之前,不会在新位置重新绘制内部框架的内部。The default behavior (called live dragging) is to reposition and repaint some or all of internal frame continuously while it is being moved; this can be slow if the desktop has many internal frames.默认行为(称为实时拖动)是在移动内部帧的同时连续重新定位和重新绘制部分或全部内部帧;如果桌面有许多内部框架,这可能会很慢。

Use the JDesktopPane method setDragMode* to specify outline dragging. 使用JDesktopPane方法setDragMode*指定轮廓拖动。For example:

desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);

The Internal Frame API内部框架API

The following tables list the commonly used JInternalFrame constructors and methods, as well as a few methods that JDesktopPane provides. 下表列出了常用的JInternalFrame构造函数和方法,以及JDesktopPane提供的一些方法。Besides the API listed in this section, JInternalFrame inherits useful API from its superclasses, JComponent, Component, and Container. 除了本节列出的API之外,JInternalFrame还从其超类JComponentComponentContainer继承了有用的API。See The JComponent Class for lists of methods from those classes.有关这些类的方法列表,请参阅JComponent

Like JInternalFrame, JDesktopPane descends from JComponent, and thus provides the methods described in The JComponent Class. JInternalFrame一样,JDesktopPaneJComponent派生,因此提供了JComponnt类中描述的方法。Because JDesktopPane extends JLayeredPane, it also supports the methods described in The Layered Pane API.因为JDesktopPane扩展了JLayeredPane,所以它还支持分层窗格API中描述的方法。

The API for using internal frames falls into these categories:使用内部框架的API分为以下几类:

Creating the Internal Frame创建内部框架
Constructor or Method构造函数或方法 Purpose目的
JInternalFrame()
JInternalFrame(String)
JInternalFrame(String, boolean)
JInternalFrame(String, boolean, boolean)
JInternalFrame(String, boolean, boolean, boolean)
JInternalFrame(String, boolean, boolean, boolean, boolean)
Create a JInternalFrame instance. 创建一个JInternalFrame实例。The first argument specifies the title (if any) to be displayed by the internal frame. 第一个参数指定内部框架要显示的标题(如果有)。The rest of the arguments specify whether the internal frame should contain decorations allowing the user to resize, close, maximize, and iconify the internal frame (specified in that order). 其余参数指定内部框架是否应包含允许用户调整大小、关闭、最大化和图标化内部框架的装饰(按该顺序指定)。The default value for each boolean argument is false, which means that the operation is not allowed.每个布尔参数的默认值为false,这意味着不允许该操作。
static int showInternalConfirmDialog(Component, Object)
static String showInternalInputDialog(Component, Object)
static Object showInternalMessageDialog(Component, Object)
static int showInternalOptionDialog(Component, Object, String, int, int, Icon, Object[], Object)
Create a JInternalFrame that simulates a dialog. 创建一个模拟对话框的JInternalFrameSee How to Make Dialogs for details.有关详细信息,请参阅如何创建对话框
Adding Components to the Internal Frame向内部框架添加组件
Method方法 Purpose目的
void setContentPane(Container)
Container getContentPane()
Set or get the internal frame's content pane, which generally contains all of the internal frame's GUI, with the exception of the menu bar and window decorations.设置或获取内部框架的内容窗格,该窗格通常包含所有内部框架的GUI,但菜单栏和窗口装饰除外。
void setJMenuBar(JMenuBar)
JMenuBar getJMenuBar()
Set or get the internal frame's menu bar.设置或获取内部框架的菜单栏。
void setLayeredPane(JLayeredPane)
JLayeredPane getLayeredPane()
Set or get the internal frame's layered pane.设置或获取内部框架的分层窗格。
Specifying the Internal Frame's Visibility, Size, and Location指定内部框架的可见性、大小和位置
Method方法 Purpose目的
void setVisible(boolean) Make the internal frame visible (if true) or invisible (if false). 使内部框架可见(如果为true)或不可见(如果是false)。You should invoke setVisible(true) on each JInternalFrame before adding it to its container. 在将每个JInternalFrame添加到其容器之前,应在其上调用setVisible(true)(Inherited from Component).(从Component继承)。
void pack() Size the internal frame so that its components are at their preferred sizes.调整内部框架的尺寸,使其组件处于其首选尺寸。
void setLocation(Point)
void setLocation(int, int)
Set the position of the internal frame. 设置内部框架的位置。(Inherited from Component).(从Component继承)。
void setBounds(Rectangle)
void setBounds(int, int, int, int)
Explicitly set the size and location of the internal frame. 显式设置内部框架的大小和位置。(Inherited from Component).(从Component继承)。
void setSize(Dimension)
void setSize(int, int)
Explicitly set the size of the internal frame. 显式设置内部框架的大小。(Inherited from Component).(从Component继承)。
Performing Window Operations on the Internal Frame在内部框架上执行窗口操作
Method方法 Purpose目的
void setDefaultCloseOperation(int)
int getDefaultCloseOperation()
Set or get what the internal frame does when the user attempts to "close" the internal frame. 设置或获取用户试图“关闭”内部框架时内部框架的操作。The default value is DISPOSE_ON_CLOSE. 默认值为DISPOSE_ON_CLOSEOther possible values are DO_NOTHING_ON_CLOSE and HIDE_ON_CLOSE. 其他可能的值有DO_NOTHING_ON_CLOSEHIDE_ON_CLOSESee Responding to Window-Closing Events for details.有关详细信息,请参阅响应窗口关闭事件
void addInternalFrameListener(InternalFrameListener)
void removeInternalFrameListener(InternalFrameListener)
Add or remove an internal frame listener (JInternalFrame's equivalent of a window listener). 添加或删除内部帧侦听器(JInternalFrame相当于窗口侦听器)。See How to Write an Internal Frame Listener for more information.有关详细信息,请参阅如何编写内部帧侦听器
void moveToFront()
void moveToBack()
If the internal frame's parent is a layered pane such as a desktop pane, moves the internal frame to the front or back (respectively) of its layer.如果内部框架的父级是分层窗格(如桌面窗格),则将内部框架移动到其层的前部或后部(分别)。
void setClosed(boolean)
boolean isClosed()
Set or get whether the internal frame is currently closed. 设置或获取内部框架当前是否关闭。The argument to setClosed must be true. setClosed的参数必须为trueWhen reopening a closed internal frame, you make it visible and add it to a container (usually the desktop pane you originally added it to).重新打开关闭的内部框架时,将其显示并将其添加到容器(通常是最初添加到的桌面窗格)。
void setIcon(boolean)
boolean isIcon()
Iconify or deiconify the internal frame, or determine whether it is currently iconified.将内部框架图标化或去图标化,或确定其当前是否图标化。
void setMaximum(boolean)
boolean isMaximum()
Maximize or restore the internal frame, or determine whether it is maximized.最大化或恢复内部帧,或确定其是否最大化。
void setSelected(boolean)
boolean isSelected()
Set or get whether the internal frame is the currently "selected" (activated) internal frame.设置或获取内部帧是否为当前“选定”(激活)的内部帧。
Controlling Window Decorations and Capabilities控制窗口装饰和功能
Method方法 Purpose目的
void setFrameIcon(Icon)
Icon getFrameIcon()
Set or get the icon displayed in the title bar of the internal frame (usually in the top-left corner).设置或获取内部框架标题栏中显示的图标(通常在左上角)。
void setClosable(boolean)
boolean isClosable()
Set or get whether the user can close the internal frame.设置或获取用户是否可以关闭内部框架。
void setIconifiable(boolean)
boolean isIconifiable()
Set or get whether the internal frame can be iconified.设置或获取内部框架是否可以图标化。
void setMaximizable(boolean)
boolean isMaximizable()
Set or get whether the user can maximize this internal frame.设置或获取用户是否可以最大化此内部帧。
void setResizable(boolean)
boolean isResizable()
Set or get whether the internal frame can be resized.设置或获取是否可以调整内部帧的大小。
void setTitle(String)
String getTitle()
Set or get the window title.设置或获取窗口标题。
Using the JDesktopPane API使用JDesktopPane API
Constructor or Method构造函数或方法 Purpose目的
JDesktopPane() Creates a new instance of JDesktopPane.创建JDesktopPane的新实例。
JInternalFrame[] getAllFrames() Returns all JInternalFrame objects that the desktop contains.返回桌面包含的所有JInternalFrame对象。
JInternalFrame[] getAllFramesInLayer(int) Returns all JInternalFrame objects that the desktop contains that are in the specified layer. 返回桌面包含的指定层中的所有JInternalFrame对象。See How to Use Layered Panes for information about layers.有关层的信息,请参阅如何使用分层窗格
void setDragMode(int)
int getDragMode()
Set or get the drag mode used for internal frames in this desktop. 设置或获取此桌面中用于内部框架的拖动模式。The integer can be either JDesktopPane.LIVE_DRAG_MODE or JDesktopPane.OUTLINE_DRAG_MODE. 整数可以是JDesktopPane.LIVE_DRAG_MODEJDesktopPane.OUTLINE_DRAG_MODEThe default for the Java look and feel is live-drag mode.Java外观的默认设置是实时拖动模式。

Examples that Use Internal Frames使用内部框架的示例

The following examples use internal frames. 以下示例使用内部框架。Because internal frames are similar to regular frames, you should also look at Examples that Use Frames.因为内部框架与常规框架类似,所以还应该查看使用框架的示例

Example示例 Where Described描述位置 Notes备注
MyInternalFrame This page.这一页。 Implements an internal frame that appears at an offset to the previously created internal frame.实现一个内部帧,该帧显示在与先前创建的内部帧的偏移处。
InternalFrameDemo This page.这一页。 Lets you create internal frames (instances of MyInternalFrame) that go into the application's JDesktopPane.允许您创建进入应用程序JDesktopPane的内部框架(MyInternalFrame的实例)。
InternalFrameEventDemo How to Write an Internal Frame Listener如何编写内部帧侦听器 Demonstrates listening for internal frame events. 演示侦听内部帧事件。Also demonstrates positioning internal frames within a desktop pane.还演示如何在桌面窗格中定位内部框架。

Previous page: How to Make Frames (Main Windows)
Next page: How to Use Labels