- 所有已实现的接口:
ImageObserver,MenuContainer,Serializable,Accessible,RootPaneContainer,WindowConstants
java.awt.Frame 的扩展版本,增加了对 JFC/Swing 组件架构的支持。您可以在 如何制作相框 部分找到有关在 The Java Tutorial 中使用 JFrame 的面向任务的文档。
JFrame 类与 Frame 略有不兼容。与所有其他 JFC/Swing 顶级容器一样,JFrame 包含一个 JRootPane 作为其唯一的子容器。这内容面板通常,根窗格提供的应该包含 JFrame 显示的所有非菜单组件。这与 AWT Frame 案例不同。为方便起见,覆盖了此类的 add、remove 和 setLayout 方法,以便它们将调用委托给 ContentPane 的相应方法。例如,您可以将子组件添加到框架中,如下所示:
frame.add(child);并且孩子将被添加到 contentPane。内容窗格将始终为非空。尝试将其设置为 null 将导致 JFrame 引发异常。默认内容窗格将在其上设置 BorderLayout 管理器。有关添加、删除和设置
JFrame 的 LayoutManager 的详细信息,请参阅 RootPaneContainer 。
与 Frame 不同,JFrame 具有一些关于在用户尝试关闭窗口时如何响应的概念。默认行为是在用户关闭窗口时简单地隐藏 JFrame。要更改默认行为,您可以调用方法 setDefaultCloseOperation(int) 。要使 JFrame 的行为与 Frame 实例相同,请使用 setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)。
有关内容窗格和根窗格提供的其他功能的更多信息,请参阅 The Java Tutorial 中的 使用顶级容器。
在多屏环境中,您可以在不同的屏幕设备上创建JFrame。有关详细信息,请参阅 Frame 。
Warning: Swing 不是线程安全的。有关详细信息,请参阅 Swing 的线程策略。
Warning: 此类的序列化对象将与未来的 Swing 版本不兼容。当前的序列化支持适用于运行相同版本 Swing 的应用程序之间的短期存储或 RMI。从 1.4 开始,对所有 JavaBeans 的长期存储的支持已添加到 java.beans 包中。请参阅 XMLEncoder 。
- 自从:
- 1.2
- 参见:
-
内部类总结
内部类在类 java.awt.Frame 中声明的嵌套类/接口
Frame.AccessibleAWTFrame在类 java.awt.Window 中声明的嵌套类/接口
Window.AccessibleAWTWindow, Window.Type在类 java.awt.Container 中声明的嵌套类/接口
Container.AccessibleAWTContainer -
字段摘要
字段修饰符和类型Field描述protected AccessibleContext可访问的上下文属性。protected JRootPane管理该帧的contentPane和可选的menuBar以及glassPane的JRootPane实例。protected boolean如果为真,则对add和setLayout的调用将被转发到contentPane。在类 java.awt.Frame 中声明的字段
CROSSHAIR_CURSOR, DEFAULT_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, ICONIFIED, MAXIMIZED_BOTH, MAXIMIZED_HORIZ, MAXIMIZED_VERT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NORMAL, NW_RESIZE_CURSOR, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, W_RESIZE_CURSOR, WAIT_CURSOR在类 java.awt.Component 中声明的字段
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT在接口 java.awt.image.ImageObserver 中声明的字段
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH在接口 javax.swing.WindowConstants 中声明的字段
DISPOSE_ON_CLOSE, DO_NOTHING_ON_CLOSE, EXIT_ON_CLOSE, HIDE_ON_CLOSE -
构造方法总结
构造方法构造方法描述JFrame()构造一个最初不可见的新框架。在屏幕设备的指定GraphicsConfiguration中创建一个Frame和一个空白标题。创建一个新的、最初不可见的具有指定标题的Frame。JFrame(String title, GraphicsConfiguration gc) 创建具有指定标题和屏幕设备的指定GraphicsConfiguration的JFrame。 -
方法总结
修饰符和类型方法描述protected void添加指定的孩子Component。protected JRootPane由构造方法调用以创建默认的rootPane。protected void由构造函数调用以正确初始化JFrame。获取与此 JFrame 关联的 AccessibleContext。返回此帧的contentPane对象。int返回用户在此框架上启动“关闭”时发生的操作。返回此帧的glassPane对象。为此组件创建图形上下文。返回在此框架上设置的菜单栏。返回此帧的layeredPane对象。返回此帧的rootPane对象。获取transferHandler属性。static boolean如果新创建的JFrame应具有由当前外观提供的 Window 装饰,则返回 true。protected boolean返回对add和setLayout的调用是否转发到contentPane。protected String返回此JFrame的字符串表示形式protected void处理此组件上发生的窗口事件。void从容器中移除指定的组件。voidrepaint(long time, int x, int y, int width, int height) 在time毫秒内重新绘制此组件的指定矩形。voidsetContentPane(Container contentPane) 设置contentPane属性。voidsetDefaultCloseOperation(int operation) 设置当用户在此框架上启动“关闭”时默认发生的操作。static voidsetDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) 提供有关新创建的JFrame是否应具有由当前外观提供的窗口装饰(例如边框、关闭窗口的小部件、标题...)的提示。voidsetGlassPane(Component glassPane) 设置glassPane属性。voidsetJMenuBar(JMenuBar menubar) 设置此框架的菜单栏。voidsetLayeredPane(JLayeredPane layeredPane) 设置layeredPane属性。voidsetLayout(LayoutManager manager) 设置LayoutManager。protected voidsetRootPane(JRootPane root) 设置rootPane属性。protected voidsetRootPaneCheckingEnabled(boolean enabled) 设置是否将对add和setLayout的调用转发到contentPane。voidsetTransferHandler(TransferHandler newHandler) 设置transferHandler属性,这是一种支持将数据传输到此组件的机制。void只需调用paint(g)。在类 java.awt.Frame 中声明的方法
addNotify, getCursorType, getExtendedState, getFrames, getIconImage, getMaximizedBounds, getMenuBar, getState, getTitle, isResizable, isUndecorated, remove, removeNotify, setCursor, setExtendedState, setMaximizedBounds, setMenuBar, setResizable, setState, setTitle, setUndecorated在类 java.awt.Window 中声明的方法
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBackground, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getIconImages, getInputContext, getListeners, getLocale, getModalExclusionType, getMostRecentFocusOwner, getOpacity, getOwnedWindows, getOwner, getOwnerlessWindows, getShape, getToolkit, getType, getWarningString, getWindowFocusListeners, getWindowListeners, getWindows, getWindowStateListeners, hide, isActive, isAlwaysOnTop, isAlwaysOnTopSupported, isAutoRequestFocus, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isOpaque, isShowing, isValidateRoot, pack, paint, postEvent, processEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, reshape, setAlwaysOnTop, setAutoRequestFocus, setBackground, setBounds, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setIconImage, setIconImages, setLocation, setLocation, setLocationByPlatform, setLocationRelativeTo, setMinimumSize, setModalExclusionType, setOpacity, setShape, setSize, setSize, setType, setVisible, show, toBack, toFront在类 java.awt.Container 中声明的方法
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusDownCycle, validate, validateTree在类 java.awt.Component 中声明的方法
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, resize, resize, revalidate, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setMaximumSize, setMixingCutoutShape, setName, setPreferredSize, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle在类 java.lang.Object 中声明的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait在接口 java.awt.MenuContainer 中声明的方法
getFont, postEvent
-
字段详细信息
-
rootPane
管理该帧的contentPane和可选的menuBar以及glassPane的JRootPane实例。- 参见:
-
rootPaneCheckingEnabled
protected boolean rootPaneCheckingEnabled如果为真,则对add和setLayout的调用将被转发到contentPane。这最初是 false,但在构造JFrame时设置为 true。- 参见:
-
accessibleContext
可访问的上下文属性。
-
-
构造方法详细信息
-
JFrame
构造一个最初不可见的新框架。此构造函数将组件的locale属性设置为
JComponent.getDefaultLocale返回的值。- 抛出:
HeadlessException- 如果 GraphicsEnvironment.isHeadless() 返回 true。- 参见:
-
JFrame
在屏幕设备的指定GraphicsConfiguration中创建一个Frame和一个空白标题。此构造函数将组件的locale属性设置为
JComponent.getDefaultLocale返回的值。- 参数:
gc- 用于构建新的Frame的GraphicsConfiguration;如果gc是null,则假定系统默认值GraphicsConfiguration- 抛出:
IllegalArgumentException- 如果gc不是来自屏幕设备。当 GraphicsEnvironment.isHeadless() 返回 true 时,总是抛出此异常。- 自从:
- 1.3
- 参见:
-
JFrame
创建一个新的、最初不可见的具有指定标题的Frame。此构造函数将组件的locale属性设置为
JComponent.getDefaultLocale返回的值。- 参数:
title- 框架的标题- 抛出:
HeadlessException- 如果 GraphicsEnvironment.isHeadless() 返回 true。- 参见:
-
JFrame
创建具有指定标题和屏幕设备的指定GraphicsConfiguration的JFrame。此构造函数将组件的locale属性设置为
JComponent.getDefaultLocale返回的值。- 参数:
title- 要显示在框架边框中的标题。null值被视为空字符串“”。gc- 用于构造新的JFrame的GraphicsConfiguration;如果gc是null,则假定系统默认值GraphicsConfiguration- 抛出:
IllegalArgumentException- 如果gc不是来自屏幕设备。当 GraphicsEnvironment.isHeadless() 返回 true 时,总是抛出此异常。- 自从:
- 1.3
- 参见:
-
-
方法详情
-
frameInit
protected void frameInit()由构造函数调用以正确初始化JFrame。 -
createRootPane
由构造方法调用以创建默认的rootPane。- 返回:
-
一个新的
JRootPane
-
processWindowEvent
处理此组件上发生的窗口事件。根据defaultCloseOperation属性的设置指定隐藏窗口或丢弃它。- 重写:
processWindowEvent在类Window中- 参数:
e- 窗口事件- 参见:
-
setDefaultCloseOperation
@BeanProperty (preferred =true, enumerationValues ={"WindowConstants.DO_NOTHING_ON_CLOSE","WindowConstants.HIDE_ON_CLOSE","WindowConstants.DISPOSE_ON_CLOSE","WindowConstants.EXIT_ON_CLOSE"}, description ="The frame\'s default close operation.") public void setDefaultCloseOperation(int operation) 设置当用户在此框架上启动“关闭”时默认发生的操作。您必须指定以下选项之一:
DO_NOTHING_ON_CLOSE(在WindowConstants中定义):什么都不做;要求程序处理已注册WindowListener对象的windowClosing方法中的操作。HIDE_ON_CLOSE(在WindowConstants中定义):调用任何已注册的WindowListener对象后自动隐藏框架。DISPOSE_ON_CLOSE(在WindowConstants中定义):在调用任何已注册的WindowListener对象后自动隐藏和处理框架。EXIT_ON_CLOSE(在WindowConstants中定义):使用Systemexit方法退出应用程序。仅在应用程序中使用它。
默认情况下,该值设置为
HIDE_ON_CLOSE。更改此属性的值会触发属性更改事件,属性名称为“defaultCloseOperation”。笔记:当 Java 虚拟机 (VM) 中的最后一个可显示窗口被处理掉时,VM 可能会终止。有关详细信息,请参阅 AWT 线程问题。
- 参数:
operation- 用户关闭框架时应该执行的操作- 抛出:
IllegalArgumentException- 如果 defaultCloseOperation 值不是上述有效值之一SecurityException- 如果指定了EXIT_ON_CLOSE并且SecurityManager将不允许调用者调用System.exit- 参见:
-
getDefaultCloseOperation
public int getDefaultCloseOperation()返回用户在此框架上启动“关闭”时发生的操作。- 返回:
- 指示窗口关闭操作的整数
- 参见:
-
setTransferHandler
@BeanProperty (hidden =true, description ="Mechanism for transfer of data into the component") public void setTransferHandler(TransferHandler newHandler) 设置transferHandler属性,这是一种支持将数据传输到此组件的机制。如果组件不支持数据传输操作,请使用null。如果系统属性
suppressSwingDropSupport是false(默认值)并且此组件上的当前放置目标是null或不是用户设置的放置目标,则此方法将按如下方式更改放置目标:如果newHandler是null它将清除放置目标。如果不是null,它将安装一个新的DropTarget。注意:与
JFrame一起使用时,TransferHandler仅提供数据导入功能,因为数据导出相关方法当前类型为JComponent。请参阅 如何使用拖放和数据传输,The Java Tutorial 中的一个部分,了解更多信息。
- 参数:
newHandler- 新的TransferHandler- 自从:
- 1.6
- 参见:
-
getTransferHandler
获取transferHandler属性。- 返回:
transferHandler属性的值- 自从:
- 1.6
- 参见:
-
update
只需调用paint(g)。重写此方法以防止不必要的调用来清除背景。 -
setJMenuBar
@BeanProperty (bound =false, hidden =true, description ="The menubar for accessing pulldown menus from this frame.") public void setJMenuBar(JMenuBar menubar) 设置此框架的菜单栏。- 参数:
menubar- 菜单栏被放置在框架中- 参见:
-
getJMenuBar
返回在此框架上设置的菜单栏。- 返回:
- 此框架的菜单栏
- 参见:
-
isRootPaneCheckingEnabled
protected boolean isRootPaneCheckingEnabled()返回对add和setLayout的调用是否转发到contentPane。- 返回:
-
如果
add和setLayout被转发则为真;否则为假 - 参见:
-
setRootPaneCheckingEnabled
@BeanProperty (hidden =true, description ="Whether the add and setLayout methods are forwarded") protected void setRootPaneCheckingEnabled(boolean enabled) 设置是否将对add和setLayout的调用转发到contentPane。- 参数:
enabled- 如果转发add和setLayout则为真,如果它们应直接在JFrame上操作则为假。- 参见:
-
addImpl
添加指定的孩子Component。此方法被覆盖以有条件地将调用转发给contentPane。默认情况下,孩子被添加到contentPane而不是框架,有关详细信息,请参阅RootPaneContainer。- 重写:
addImpl在类Container中- 参数:
comp- 要增强的组件constraints- 要遵守的约束index- 索引- 抛出:
IllegalArgumentException- 如果index无效IllegalArgumentException- 如果将容器的父级添加到自身IllegalArgumentException- 如果向容器添加窗口- 参见:
-
remove
从容器中移除指定的组件。如果comp不是rootPane,这会将调用转发给contentPane。如果comp不是JFrame或contentPane的孩子,这将不执行任何操作。- 重写:
remove在类Container中- 参数:
comp- 要删除的组件- 抛出:
NullPointerException- 如果comp为空- 参见:
-
setLayout
-
getRootPane
@BeanProperty (bound =false, hidden =true, description ="the RootPane object for this frame.") public JRootPane getRootPane()返回此帧的rootPane对象。- 指定者:
getRootPane在接口RootPaneContainer中- 返回:
rootPane属性- 参见:
-
setRootPane
设置rootPane属性。该方法由构造函数调用。- 参数:
root- 此帧的rootPane对象- 参见:
-
getContentPane
返回此帧的contentPane对象。- 指定者:
getContentPane在接口RootPaneContainer中- 返回:
contentPane属性- 参见:
-
setContentPane
@BeanProperty (bound =false, hidden =true, description ="The client area of the frame where child components are normally inserted.") public void setContentPane(Container contentPane) 设置contentPane属性。该方法由构造函数调用。Swing 的绘画架构需要包含层次结构中的不透明
JComponent。这通常由内容窗格提供。如果您替换内容窗格,建议您将其替换为不透明的JComponent。- 指定者:
setContentPane在接口RootPaneContainer中- 参数:
contentPane- 此帧的contentPane对象- 抛出:
IllegalComponentStateException-(运行时异常)如果内容窗格参数为null- 参见:
-
getLayeredPane
返回此帧的layeredPane对象。- 指定者:
getLayeredPane在接口RootPaneContainer中- 返回:
layeredPane属性- 参见:
-
setLayeredPane
@BeanProperty (bound =false, hidden =true, description ="The pane that holds the various frame layers.") public void setLayeredPane(JLayeredPane layeredPane) 设置layeredPane属性。该方法由构造函数调用。- 指定者:
setLayeredPane在接口RootPaneContainer中- 参数:
layeredPane- 此帧的layeredPane对象- 抛出:
IllegalComponentStateException-(运行时异常)如果分层窗格参数为null- 参见:
-
getGlassPane
返回此帧的glassPane对象。- 指定者:
getGlassPane在接口RootPaneContainer中- 返回:
glassPane属性- 参见:
-
setGlassPane
@BeanProperty (bound =false, hidden =true, description ="A transparent pane used for menu rendering.") public void setGlassPane(Component glassPane) 设置glassPane属性。该方法由构造函数调用。- 指定者:
setGlassPane在接口RootPaneContainer中- 参数:
glassPane- 此帧的glassPane对象- 参见:
-
getGraphics
为此组件创建图形上下文。如果此组件当前不可显示,此方法将返回null。- 重写:
getGraphics在类Component中- 返回:
-
此组件的图形上下文,如果没有则为
null - 自从:
- 1.6
- 参见:
-
repaint
public void repaint(long time, int x, int y, int width, int height) 在time毫秒内重新绘制此组件的指定矩形。有关如何处理重绘的详细信息,请参阅RepaintManager。 -
setDefaultLookAndFeelDecorated
public static void setDefaultLookAndFeelDecorated(boolean defaultLookAndFeelDecorated) 提供有关新创建的JFrame是否应具有由当前外观提供的窗口装饰(例如边框、关闭窗口的小部件、标题...)的提示。如果defaultLookAndFeelDecorated为真,当前的LookAndFeel支持提供窗口装饰,并且当前的窗口管理器支持未装饰的窗口,那么新创建的JFrame的窗口装饰将由当前的LookAndFeel提供。否则,新创建的JFrame将具有由当前窗口管理器提供的窗口装饰。您可以通过执行以下操作在单个 JFrame 上获得相同的效果:
JFrame frame = new JFrame(); frame.setUndecorated(true); frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
- 参数:
defaultLookAndFeelDecorated- 关于当前外观是否应提供窗口装饰的提示- 自从:
- 1.4
- 参见:
-
isDefaultLookAndFeelDecorated
public static boolean isDefaultLookAndFeelDecorated()如果新创建的JFrame应具有由当前外观提供的 Window 装饰,则返回 true。这只是一个提示,因为某些外观可能不支持此功能。- 返回:
- 如果外观应该提供 Window 装饰,则为 true。
- 自从:
- 1.4
-
paramString
返回此JFrame的字符串表示形式。此方法仅用于调试目的,返回字符串的内容和格式可能因实现而异。返回的字符串可能为空,但可能不是null。- 重写:
paramString在类Frame中- 返回:
-
这个
JFrame的字符串表示
-
getAccessibleContext
获取与此 JFrame 关联的 AccessibleContext。对于 JFrames,AccessibleContext 采用 AccessibleJFrame 的形式。如有必要,将创建一个新的 AccessibleJFrame 实例。- 指定者:
getAccessibleContext在接口Accessible中- 重写:
getAccessibleContext在类Frame中- 返回:
- 作为此 JFrame 的 AccessibleContext 的 AccessibleJFrame
-