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发行说明。
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:以下是一个应用程序的图片,该应用程序在一个常规框架内有两个内部框架(其中一个是图标化的):
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); }
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 还提供了其他API,如pack
, that makes it similar to JFrame
.pack
,使其类似于JFrame
。
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
)时才有效。
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
类。
setSize
, pack
, or setBounds
.setSize
、pack
或setBounds
。setLocation
or setBounds
method to specify the upper left point of the internal frame, relative to its container.setLocation
或setBounds
方法指定内部框架相对于其容器的左上点。JFrame
situation. JFrame
的情况完全相同。JOptionPane
or JInternalFrame
, not JDialog
.JOptionPane
或JInternalFrame
实现,而不是JDialog
。JOptionPane
showInternalXxxDialog
methods, as described in How to Make Dialogs.JOptionPane
showInternalXxxDialog
方法,如如何创建对话框中所述。JDesktopPane
), the internal frame will not appear.JDesktopPane
),则不会显示内部框架。show
or setVisible
on internal frames.show
或setVisible
。setVisible(true)
or show()
to make them visible.setVisible(true)
或show()
使它们可见。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 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, 除了本节列出的API之外,JInternalFrame
inherits useful API from its superclasses, JComponent
, Component
, and Container
. JInternalFrame
还从其超类JComponent
、Component
和Container
继承了有用的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
一样,JDesktopPane
从JComponent
派生,因此提供了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分为以下几类:
JDesktopPane
APIJInternalFrame() JInternalFrame(String) JInternalFrame(String, boolean) JInternalFrame(String, boolean, boolean) JInternalFrame(String, boolean, boolean, boolean) JInternalFrame(String, boolean, boolean, boolean, boolean) |
JInternalFrame instance. JInternalFrame 实例。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) |
JInternalFrame that simulates a dialog. JInternalFrame 。 |
void setContentPane(Container) Container getContentPane() |
|
void setJMenuBar(JMenuBar) JMenuBar getJMenuBar() |
|
void setLayeredPane(JLayeredPane) JLayeredPane getLayeredPane() |
void setVisible(boolean) |
true ) or invisible (if false ). true )或不可见(如果是false )。setVisible(true) on each JInternalFrame before adding it to its container. JInternalFrame 添加到其容器之前,应在其上调用setVisible(true) 。Component ).Component 继承)。 |
void pack() |
|
void setLocation(Point) void setLocation(int, int) |
Component ).Component 继承)。 |
void setBounds(Rectangle) void setBounds(int, int, int, int) |
Component ).Component 继承)。 |
void setSize(Dimension) void setSize(int, int) |
Component ).Component 继承)。 |
void setDefaultCloseOperation(int) int getDefaultCloseOperation() |
DISPOSE_ON_CLOSE . DISPOSE_ON_CLOSE 。DO_NOTHING_ON_CLOSE and HIDE_ON_CLOSE . DO_NOTHING_ON_CLOSE 和HIDE_ON_CLOSE 。 |
void addInternalFrameListener(InternalFrameListener) void removeInternalFrameListener(InternalFrameListener) |
JInternalFrame 's equivalent of a window listener). JInternalFrame 相当于窗口侦听器)。 |
void moveToFront() void moveToBack() |
|
void setClosed(boolean) boolean isClosed() |
setClosed must be true . setClosed 的参数必须为true 。 |
void setIcon(boolean) boolean isIcon() |
|
void setMaximum(boolean) boolean isMaximum() |
|
void setSelected(boolean) boolean isSelected() |
void setFrameIcon(Icon) Icon getFrameIcon() |
|
void setClosable(boolean) boolean isClosable() |
|
void setIconifiable(boolean) boolean isIconifiable() |
|
void setMaximizable(boolean) boolean isMaximizable() |
|
void setResizable(boolean) boolean isResizable() |
|
void setTitle(String) String getTitle() |
JDesktopPane() |
JDesktopPane .JDesktopPane 的新实例。 |
JInternalFrame[] getAllFrames() |
JInternalFrame objects that the desktop contains.JInternalFrame 对象。 |
JInternalFrame[] getAllFramesInLayer(int) |
JInternalFrame objects that the desktop contains that are in the specified layer. JInternalFrame 对象。 |
void setDragMode(int) int getDragMode() |
JDesktopPane.LIVE_DRAG_MODE or JDesktopPane.OUTLINE_DRAG_MODE . JDesktopPane.LIVE_DRAG_MODE 或JDesktopPane.OUTLINE_DRAG_MODE 。 |
The following examples use internal frames. 以下示例使用内部框架。Because internal frames are similar to regular frames, you should also look at Examples that Use Frames.因为内部框架与常规框架类似,所以还应该查看使用框架的示例。
MyInternalFrame |
||
InternalFrameDemo |
MyInternalFrame ) that go into the application's JDesktopPane .JDesktopPane 的内部框架(MyInternalFrame 的实例)。 | |
InternalFrameEventDemo |