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发行说明。
A Frame is a top-level window with a title and a border. 框架是带有标题和边框的顶级窗口。The size of the frame includes any area designated for the border. 框架的大小包括为边界指定的任何区域。The dimensions of the border area may be obtained using the 可以使用getInsets
method. getInsets
方法获得边界区域的尺寸。Since the border area is included in the overall size of the frame, the border effectively obscures a portion of the frame, constraining the area available for rendering and/or displaying subcomponents to the rectangle which has an upper-left corner location of 由于边框区域包含在框架的整体大小中,因此边框有效地遮挡了框架的一部分,将可用于渲染和/或显示子组件的区域限制为矩形,该矩形的左上角位置为(insets.left
, insets.top)
, and has a size of width - (insets.left + insets.right)
by height - (insets.top + insets.bottom)
.(insets.left, insets.top)
,且其大小为width - (insets.left + insets.right)
乘以height - (insets.top + insets.bottom)
。
A frame, implemented as an instance of the 框架实现为JFrame
class, is a window that has decorations such as a border, a title, and supports button components that close or iconify the window. JFrame
类的一个实例,是一个具有边框、标题等装饰的窗口,并支持关闭或图标化窗口的按钮组件。Applications with a GUI usually include at least one frame. 具有GUI的应用程序通常至少包括一个框架。Applets sometimes use frames, as well.小程序有时也使用框架。
To make a window that is dependent on another window disappearing when the other window is iconified, for example use a 以使窗口依赖于另一窗口当另一个窗口被图标化时消失 使用dialog
instead of frame.
. dialog
而不是frame
。To make a window that appears within another window, use an internal frame. 要使窗口显示在另一个窗口中,请使用内部框架。
Here is a picture of the extremely plain window created by the 下面是FrameDemo
demonstration application. FrameDemo
演示应用程序创建的极其简单的窗口的图片。You can find the source code in 您可以在FrameDemo.java
. FrameDemo.java
中找到源代码。You can run FrameDemo ( download JDK 7 or later).您可以运行FrameDemo(下载JDK 7或更高版本)。
The following 下面的FrameDemo
code shows how to create and set up a frame.FrameDemo
代码展示了如何创建和设置框架。
//1. Create the frame. JFrame frame = new JFrame("FrameDemo"); //2. Optional: What happens when the frame closes? frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //3. Create components and put them in the frame. //...create emptyLabel... frame.getContentPane().add(emptyLabel, BorderLayout.CENTER); //4. Size the frame. frame.pack(); //5. Show it. frame.setVisible(true);
Here are some details about the code:以下是代码的一些详细信息:
JFrame
constructor is the no-argument constructor.JFrame
构造函数是无参数构造函数。EXIT_ON_CLOSE
operation exits the program when your user closes the frame. EXIT_ON_CLOSE
操作退出程序。See Responding to Window-Closing Events for more information.有关详细信息,请参阅响应窗口关闭事件。
For frames that have menus, you'd typically add the menu bar to the frame here using the 对于具有菜单的框架,通常使用setJMenuBar
method. setJMenuBar
方法将菜单栏添加到框架中。See How to Use Menus for details.有关详细信息,请参阅如何使用菜单。
pack
method sizes the frame so that all its contents are at or above their preferred sizes. pack
方法确定框架的尺寸,使其所有内容物处于或高于其优选尺寸。pack
is to establish a frame size explicitly by calling setSize
or setBounds
(which also sets the frame location). pack
的另一种方法是通过调用setSize
或setBounds
(也设置帧位置)显式地建立帧大小。pack
is preferable to calling setSize
, since pack
leaves the frame layout manager in charge of the frame size, and layout managers are good at adjusting to platform dependencies and other factors that affect component size. pack
比调用setSize
更可取,因为pack
让框架布局管理器负责框架大小,布局管理器擅长调整平台依赖性和其他影响组件大小的因素。This example does not set the frame location, but it is easy to do so using either the 本例不设置帧位置,但使用setLocationRelativeTo
or setLocation
method. For example, the following code centers a frame onscreen:setLocationRelativeTo
或setLocation
方法很容易设置。例如,以下代码在屏幕上居中显示帧:
frame.setLocationRelativeTo(null);
setVisible(true)
makes the frame appear onscreen. setVisible(true)
将使帧显示在屏幕上。show
method used instead. show
方法。setVisible(true)
for consistency's sake.setVisible(true)
。By default, window decorations are supplied by the native window system. 默认情况下,窗口装饰由本机窗口系统提供。However, you can request that the look-and-feel provide the decorations for a frame. 但是,您可以要求外观为框架提供装饰。You can also specify that the frame have no window decorations at all, a feature that can be used on its own, or to provide your own decorations, or with full-screen exclusive mode.您还可以指定框架完全没有窗口装饰,该功能可以单独使用,也可以提供您自己的装饰,或者使用全屏独占模式。
Besides specifying who provides the window decorations, you can also specify which icon is used to represent the window. 除了指定谁提供窗口装饰外,还可以指定哪个图标用于表示窗口。Exactly how this icon is used depends on the window system or look and feel that provides the window decorations. 该图标的具体使用方式取决于提供窗口装饰的窗口系统或外观。If the window system supports minimization, then the icon is used to represent the minimized window. 如果窗口系统支持最小化,则图标用于表示最小化窗口。Most window systems or look and feels also display the icon in the window decorations. 大多数窗口系统或外观也在窗口装饰中显示图标。A typical icon size is 16x16 pixels, but some window systems use other sizes.典型的图标大小为16x16像素,但某些窗口系统使用其他大小。
The following snapshots show three frames that are identical except for their window decorations. 下面的快照显示了三个相同的框架,除了它们的窗口装饰。As you can tell by the appearance of the button in each frame, all three use the Java look and feel. 从每个帧中按钮的外观可以看出,所有三个都使用Java外观。The first uses decorations provided by the window system, which happen to be Microsoft Windows, but could as easily be any other system running the Java platform.第一种使用窗口系统提供的装饰,该系统恰好是Microsoft Windows,但也可以是运行Java平台的任何其他系统。The second and third use window decorations provided by the Java look and feel. 第二个和第三个使用Java外观提供的窗口装饰。The third frame uses Java look and feel window decorations, but has a custom icon. 第三个框架使用Java外观窗口装饰,但有一个自定义图标。
![]() |
![]() |
![]() |
Here is an example of creating a frame with a custom icon and with window decorations provided by the look and feel:下面是一个创建带有自定义图标的框架的示例,以及由“外观和感觉”提供的窗口装饰:
//Ask for window decorations provided by the look and feel. JFrame.setDefaultLookAndFeelDecorated(true); //Create the frame. JFrame frame = new JFrame("A window"); //Set the frame icon to an image loaded from a file. frame.setIconImage(new ImageIcon(imgURL).getImage());
As the preceding code snippet implies, you must invoke the 正如前面的代码片段所暗示的,在创建要影响其装饰的框架之前,必须调用setDefaultLookAndFeelDecorated
method before creating the frame whose decorations you wish to affect. setDefaultLookAndFeelDecorated
方法。The value you set with 使用setDefaultLookAndFeelDecorated
is used for all subsequently created JFrame
s. setDefaultLookAndFeelDecorated
设置的值将用于所有随后创建的JFrame
。You can switch back to using window system decorations by invoking 通过调用JFrame.setDefaultLookAndFeelDecorated(false)
. JFrame.setDefaultLookAndFeelDecorated(false)
,可以切换回使用窗口系统装饰。Some look and feels might not support window decorations; in this case, the window system decorations are used.一些外观和感觉可能不支持窗口装饰;在这种情况下,使用窗系统装饰。
The full source code for the application that creates the frames pictured above is in 创建上述框架的应用程序的完整源代码位于FrameDemo2.java
. FrameDemo2.java
中。Besides showing how to choose window decorations, FrameDemo2 also shows how to disable all window decorations and gives an example of positioning windows. 除了展示如何选择窗口装饰外,FrameDemo2还展示了如何禁用所有窗口装饰,并给出了一个定位窗口的示例。It includes two methods that create the 它包括创建用作图标的Image
objects used as icons one is loaded from a file, and the other is painted from scratch. Image
对象的两种方法;一个从文件加载,另一个从头开始绘制。
By default, when the user closes a frame onscreen, the frame is hidden. 默认情况下,当用户关闭屏幕上的帧时,该帧将被隐藏。Although invisible, the frame still exists and the program can make it visible again. 虽然不可见,但框架仍然存在,程序可以使其再次可见。If you want different behavior, then you need to either register a window listener that handles window-closing events, or you need to specify default close behavior using the 如果需要不同的行为,则需要注册处理窗口关闭事件的窗口侦听器,或者需要使用setDefaultCloseOperation
method. setDefaultCloseOperation
方法指定默认关闭行为。You can even do both.你甚至可以同时做这两件事。
The argument to setDefaultCloseOperation
must be one of the following values, the first three of which are defined in the WindowConstants
interface (implemented by JFrame
, JInternalPane
, and JDialog
):setDefaultCloseOperation
的参数必须是以下值之一,前三个值在WindowConstants
接口中定义(由JFrame
、JInternalPane
和JDialog
实现):
DO_NOTHING_ON_CLOSE
windowClosing
method.windowClosing
方法中执行一些其他操作。HIDE_ON_CLOSE
JDialog
and JFrame
)JDialog
和JFrame
的默认值)DISPOSE_ON_CLOSE
JInternalFrame
)JInternalFrame
的默认值)EXIT_ON_CLOSE
JFrame
class)JFrame
类中定义)System.exit(0)
. System.exit(0)
退出应用程序。SecurityException
may be thrown.SecurityException
。如果屏幕上只有一个窗口,DISPOSE_ON_CLOSE
can have results similar to EXIT_ON_CLOSE
if only one window is onscreen. DISPOSE_ON_CLOSE
可能会产生类似于EXIT_ON_CLOSE
的结果。More precisely, when the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate. 更准确地说,当Java虚拟机(VM)内的最后一个可显示窗口被丢弃时,VM可以终止。See AWT Threading Issues for details.有关详细信息,请参阅AWT线程问题。
The default close operation is executed after any window listeners handle the window-closing event. 默认关闭操作在任何窗口侦听器处理窗口关闭事件后执行。So, for example, assume that you specify that the default close operation is to dispose of a frame. 因此,例如,假设您指定默认的关闭操作是处理帧。You also implement a window listener that tests whether the frame is the last one visible and, if so, saves some data and exits the application. 您还实现了一个窗口监听器,用于测试框架是否是最后一个可见的,如果是,则保存一些数据并退出应用程序。Under these conditions, when the user closes a frame, the window listener will be called first. 在这些情况下,当用户关闭帧时,将首先调用窗口侦听器。If it does not exit the application, then the default close operation disposing of the frame will then be performed.如果不退出应用程序,则默认关闭操作设置框架然后将执行。
For more information about handling window-closing events, see How to Write Window Listeners. 有关处理窗口关闭事件的详细信息,请参阅如何编写窗口侦听器。Besides handling window-closing events, window listeners can also react to other window state changes, such as iconification and activation.除了处理窗口关闭事件外,窗口侦听器还可以对其他窗口状态更改做出反应,如图标化和激活。
The following tables list the commonly used 下表列出了常用的JFrame构造函数和方法。JFrame
constructors and methods. Other methods you might want to call are defined by the 您可能需要调用的其他方法由java.awt.Frame
, java.awt.Window
, and java.awt.Component
classes, from which JFrame
descends.java.awt.Frame
、java.awt.Window
和java.awt.Component
类定义,JFrame
从这些类派生。
Because each 因为每个JFrame
object has a root pane, frames have support for interposing input and painting behavior in front of the frame children, placing children on different "layers", and for Swing menu bars. JFrame
对象都有一个根窗格,所以框架支持在框架子对象前面插入输入和绘制行为,将子对象放置在不同的“层”上,并支持Swing菜单栏。These topics are introduced in Using Top-Level Containers and explained in detail in How to Use Root Panes.这些主题在使用顶级容器中介绍,并在如何使用根窗格中详细解释。
The API for using frames falls into these categories:使用框架的API分为以下几类:
JFrame() JFrame(String)
|
String argument provides a title for the frame. String 参数提供框架的标题。setVisible(true) on it.setVisible(true) 。 |
void setDefaultCloseOperation(int) int getDefaultCloseOperation() |
WindowConstants interface, which JFrame implements. WindowConstants 接口中定义,JFrame 实现了该接口。EXIT_ON_CLOSE constant is defined in the JFrame class.EXIT_ON_CLOSE 常量在JFrame 类中定义。 |
void setIconImage(Image) Image getIconImage() (in Frame ) |
javax.swing.ImageIcon (or any other javax.swing.Icon implementation).java.awt.Image 对象,而不是javax.swing.ImageIcon (或任何其他javax.swing.Icon 实现)。 |
void setTitle(String) String getTitle() (in Frame ) |
|
void setUndecorated(boolean) boolean isUndecorated() (in Frame ) |
|
static void setDefaultLookAndFeelDecorated(boolean) static boolean isDefaultLookAndFeelDecorated() |
JFrame s should have their Window decorations (such as borders, and widgets for closing the window) provided by the current look-and-feel. JFrame 是否应具有当前外观提供的窗口装饰(如边框和用于关闭窗口的小部件)。 |
void pack() (in Window ) |
|
void setSize(int, int) void setSize(Dimension) Dimension getSize() (in Component ) |
setSize specify the width and height, respectively.setSize 的整数参数分别指定宽度和高度。 |
void setBounds(int, int, int, int) void setBounds(Rectangle) Rectangle getBounds() (in Component ) |
setBounds , the window upper left corner is at the x, y location specified by the first two arguments, and has the width and height specified by the last two arguments.setBounds ,窗口左上角位于前两个参数指定的x, y位置,并具有后两个参数所指定的宽度和高度。 |
void setLocation(int, int) Point getLocation() (in Component ) |
|
void setLocationRelativeTo(Component) (in Window ) |
null , the window is centered onscreen. null ,则窗口在屏幕上居中。 |
void setContentPane(Container) Container getContentPane() |
|
JRootPane createRootPane() void setRootPane(JRootPane) JRootPane getRootPane() |
|
void setJMenuBar(JMenuBar) JMenuBar getJMenuBar() |
|
void setGlassPane(Component) Component getGlassPane() |
|
void setLayeredPane(JLayeredPane) JLayeredPane getLayeredPane() |
All of the standalone applications in this trail use 本教程中的所有独立应用程序都使用JFrame
. JFrame
。The following table lists a few and tells you where each is discussed.下表列出了一些,并告诉您每个讨论的位置。
FrameDemo |
||
FrameDemo2 |
||
Framework |
| |
LayeredPaneDemo |
||
GlassPaneDemo |
||
MenuDemo |
JMenuBar in a JFrame .JMenuBar 放在JFrame 中。 |