Path: blob/master/src/java.desktop/share/classes/java/awt/Frame.java
41152 views
/*1* Copyright (c) 1995, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.awt;2627import java.awt.event.KeyEvent;28import java.awt.event.WindowEvent;29import java.awt.peer.FramePeer;30import java.io.IOException;31import java.io.ObjectInputStream;32import java.io.ObjectOutputStream;33import java.io.Serial;34import java.io.Serializable;35import java.util.ArrayList;36import java.util.Vector;3738import javax.accessibility.AccessibleContext;39import javax.accessibility.AccessibleRole;40import javax.accessibility.AccessibleState;41import javax.accessibility.AccessibleStateSet;4243import sun.awt.AWTAccessor;44import sun.awt.SunToolkit;4546/**47* A {@code Frame} is a top-level window with a title and a border.48* <p>49* The size of the frame includes any area designated for the50* border. The dimensions of the border area may be obtained51* using the {@code getInsets} method, however, since52* these dimensions are platform-dependent, a valid insets53* value cannot be obtained until the frame is made displayable54* by either calling {@code pack} or {@code show}.55* Since the border area is included in the overall size of the56* frame, the border effectively obscures a portion of the frame,57* constraining the area available for rendering and/or displaying58* subcomponents to the rectangle which has an upper-left corner59* location of {@code (insets.left, insets.top)}, and has a size of60* {@code width - (insets.left + insets.right)} by61* {@code height - (insets.top + insets.bottom)}.62* <p>63* The default layout for a frame is {@code BorderLayout}.64* <p>65* A frame may have its native decorations (i.e. {@code Frame}66* and {@code Titlebar}) turned off67* with {@code setUndecorated}. This can only be done while the frame68* is not {@link Component#isDisplayable() displayable}.69* <p>70* In a multi-screen environment, you can create a {@code Frame}71* on a different screen device by constructing the {@code Frame}72* with {@link #Frame(GraphicsConfiguration)} or73* {@link #Frame(String title, GraphicsConfiguration)}. The74* {@code GraphicsConfiguration} object is one of the75* {@code GraphicsConfiguration} objects of the target screen76* device.77* <p>78* In a virtual device multi-screen environment in which the desktop79* area could span multiple physical screen devices, the bounds of all80* configurations are relative to the virtual-coordinate system. The81* origin of the virtual-coordinate system is at the upper left-hand82* corner of the primary physical screen. Depending on the location83* of the primary screen in the virtual device, negative coordinates84* are possible, as shown in the following figure.85* <p>86* <img src="doc-files/MultiScreen.gif" alt="Diagram of virtual device87* encompassing three physical screens and one primary physical screen. The88* primary physical screen shows (0,0) coords while a different physical screen89* shows (-80,-100) coords." style="margin: 7px 10px;">90* <p>91* In such an environment, when calling {@code setLocation},92* you must pass a virtual coordinate to this method. Similarly,93* calling {@code getLocationOnScreen} on a {@code Frame}94* returns virtual device coordinates. Call the {@code getBounds}95* method of a {@code GraphicsConfiguration} to find its origin in96* the virtual coordinate system.97* <p>98* The following code sets the99* location of the {@code Frame} at (10, 10) relative100* to the origin of the physical screen of the corresponding101* {@code GraphicsConfiguration}. If the bounds of the102* {@code GraphicsConfiguration} is not taken into account, the103* {@code Frame} location would be set at (10, 10) relative to the104* virtual-coordinate system and would appear on the primary physical105* screen, which might be different from the physical screen of the106* specified {@code GraphicsConfiguration}.107*108* <pre>109* Frame f = new Frame(GraphicsConfiguration gc);110* Rectangle bounds = gc.getBounds();111* f.setLocation(10 + bounds.x, 10 + bounds.y);112* </pre>113*114* <p>115* Frames are capable of generating the following types of116* {@code WindowEvent}s:117* <ul>118* <li>{@code WINDOW_OPENED}119* <li>{@code WINDOW_CLOSING}:120* <br>If the program doesn't121* explicitly hide or dispose the window while processing122* this event, the window close operation is canceled.123* <li>{@code WINDOW_CLOSED}124* <li>{@code WINDOW_ICONIFIED}125* <li>{@code WINDOW_DEICONIFIED}126* <li>{@code WINDOW_ACTIVATED}127* <li>{@code WINDOW_DEACTIVATED}128* <li>{@code WINDOW_GAINED_FOCUS}129* <li>{@code WINDOW_LOST_FOCUS}130* <li>{@code WINDOW_STATE_CHANGED}131* </ul>132*133* @author Sami Shaio134* @see WindowEvent135* @see Window#addWindowListener136* @since 1.0137*/138public class Frame extends Window implements MenuContainer {139140/* Note: These are being obsoleted; programs should use the Cursor class141* variables going forward. See Cursor and Component.setCursor.142*/143144/**145* @deprecated replaced by {@code Cursor.DEFAULT_CURSOR}.146*/147@Deprecated148public static final int DEFAULT_CURSOR = Cursor.DEFAULT_CURSOR;149150151/**152* @deprecated replaced by {@code Cursor.CROSSHAIR_CURSOR}.153*/154@Deprecated155public static final int CROSSHAIR_CURSOR = Cursor.CROSSHAIR_CURSOR;156157/**158* @deprecated replaced by {@code Cursor.TEXT_CURSOR}.159*/160@Deprecated161public static final int TEXT_CURSOR = Cursor.TEXT_CURSOR;162163/**164* @deprecated replaced by {@code Cursor.WAIT_CURSOR}.165*/166@Deprecated167public static final int WAIT_CURSOR = Cursor.WAIT_CURSOR;168169/**170* @deprecated replaced by {@code Cursor.SW_RESIZE_CURSOR}.171*/172@Deprecated173public static final int SW_RESIZE_CURSOR = Cursor.SW_RESIZE_CURSOR;174175/**176* @deprecated replaced by {@code Cursor.SE_RESIZE_CURSOR}.177*/178@Deprecated179public static final int SE_RESIZE_CURSOR = Cursor.SE_RESIZE_CURSOR;180181/**182* @deprecated replaced by {@code Cursor.NW_RESIZE_CURSOR}.183*/184@Deprecated185public static final int NW_RESIZE_CURSOR = Cursor.NW_RESIZE_CURSOR;186187/**188* @deprecated replaced by {@code Cursor.NE_RESIZE_CURSOR}.189*/190@Deprecated191public static final int NE_RESIZE_CURSOR = Cursor.NE_RESIZE_CURSOR;192193/**194* @deprecated replaced by {@code Cursor.N_RESIZE_CURSOR}.195*/196@Deprecated197public static final int N_RESIZE_CURSOR = Cursor.N_RESIZE_CURSOR;198199/**200* @deprecated replaced by {@code Cursor.S_RESIZE_CURSOR}.201*/202@Deprecated203public static final int S_RESIZE_CURSOR = Cursor.S_RESIZE_CURSOR;204205/**206* @deprecated replaced by {@code Cursor.W_RESIZE_CURSOR}.207*/208@Deprecated209public static final int W_RESIZE_CURSOR = Cursor.W_RESIZE_CURSOR;210211/**212* @deprecated replaced by {@code Cursor.E_RESIZE_CURSOR}.213*/214@Deprecated215public static final int E_RESIZE_CURSOR = Cursor.E_RESIZE_CURSOR;216217/**218* @deprecated replaced by {@code Cursor.HAND_CURSOR}.219*/220@Deprecated221public static final int HAND_CURSOR = Cursor.HAND_CURSOR;222223/**224* @deprecated replaced by {@code Cursor.MOVE_CURSOR}.225*/226@Deprecated227public static final int MOVE_CURSOR = Cursor.MOVE_CURSOR;228229230/**231* Frame is in the "normal" state. This symbolic constant names a232* frame state with all state bits cleared.233* @see #setExtendedState(int)234* @see #getExtendedState235*/236public static final int NORMAL = 0;237238/**239* This state bit indicates that frame is iconified.240* @see #setExtendedState(int)241* @see #getExtendedState242*/243public static final int ICONIFIED = 1;244245/**246* This state bit indicates that frame is maximized in the247* horizontal direction.248* @see #setExtendedState(int)249* @see #getExtendedState250* @since 1.4251*/252public static final int MAXIMIZED_HORIZ = 2;253254/**255* This state bit indicates that frame is maximized in the256* vertical direction.257* @see #setExtendedState(int)258* @see #getExtendedState259* @since 1.4260*/261public static final int MAXIMIZED_VERT = 4;262263/**264* This state bit mask indicates that frame is fully maximized265* (that is both horizontally and vertically). It is just a266* convenience alias for267* <code>MAXIMIZED_VERT | MAXIMIZED_HORIZ</code>.268*269* <p>Note that the correct test for frame being fully maximized is270* <pre>271* (state & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH272* </pre>273*274* <p>To test is frame is maximized in <em>some</em> direction use275* <pre>276* (state & Frame.MAXIMIZED_BOTH) != 0277* </pre>278*279* @see #setExtendedState(int)280* @see #getExtendedState281* @since 1.4282*/283public static final int MAXIMIZED_BOTH = MAXIMIZED_VERT | MAXIMIZED_HORIZ;284285/**286* Maximized bounds for this frame.287* @see #setMaximizedBounds(Rectangle)288* @see #getMaximizedBounds289* @serial290* @since 1.4291*/292Rectangle maximizedBounds;293294295/**296* This is the title of the frame. It can be changed297* at any time. {@code title} can be null and if298* this is the case the {@code title} = "".299*300* @serial301* @see #getTitle302* @see #setTitle(String)303*/304String title = "Untitled";305306/**307* The frames menubar. If {@code menuBar} = null308* the frame will not have a menubar.309*310* @serial311* @see #getMenuBar312* @see #setMenuBar(MenuBar)313*/314MenuBar menuBar;315316/**317* This field indicates whether the frame is resizable.318* This property can be changed at any time.319* {@code resizable} will be true if the frame is320* resizable, otherwise it will be false.321*322* @serial323* @see #isResizable()324*/325boolean resizable = true;326327/**328* This field indicates whether the frame is undecorated.329* This property can only be changed while the frame is not displayable.330* {@code undecorated} will be true if the frame is331* undecorated, otherwise it will be false.332*333* @serial334* @see #setUndecorated(boolean)335* @see #isUndecorated()336* @see Component#isDisplayable()337* @since 1.4338*/339boolean undecorated = false;340341/**342* {@code mbManagement} is only used by the Motif implementation.343*344* @serial345*/346boolean mbManagement = false; /* used only by the Motif impl. */347348/**349* The bitwise mask of frame state constants.350*/351// XXX: uwe: abuse old field for now352// will need to take care of serialization353private int state = NORMAL;354355/**356* The Windows owned by the Frame.357* Note: in 1.2 this has been superseded by Window.ownedWindowList358*359* @serial360* @see java.awt.Window#ownedWindowList361*/362Vector<Window> ownedWindows;363364private static final String base = "frame";365private static int nameCounter = 0;366367/**368* Use serialVersionUID from JDK 1.1 for interoperability.369*/370@Serial371private static final long serialVersionUID = 2673458971256075116L;372373static {374/* ensure that the necessary native libraries are loaded */375Toolkit.loadLibraries();376if (!GraphicsEnvironment.isHeadless()) {377initIDs();378}379}380381/**382* Constructs a new instance of {@code Frame} that is383* initially invisible. The title of the {@code Frame}384* is empty.385* @exception HeadlessException when386* {@code GraphicsEnvironment.isHeadless()} returns {@code true}387* @see java.awt.GraphicsEnvironment#isHeadless()388* @see Component#setSize389* @see Component#setVisible(boolean)390*/391public Frame() throws HeadlessException {392this("");393}394395/**396* Constructs a new, initially invisible {@code Frame} with the397* specified {@code GraphicsConfiguration}.398*399* @param gc the {@code GraphicsConfiguration}400* of the target screen device. If {@code gc}401* is {@code null}, the system default402* {@code GraphicsConfiguration} is assumed.403* @exception IllegalArgumentException if404* {@code gc} is not from a screen device.405* @exception HeadlessException when406* {@code GraphicsEnvironment.isHeadless()} returns {@code true}407* @see java.awt.GraphicsEnvironment#isHeadless()408* @since 1.3409*/410public Frame(GraphicsConfiguration gc) {411this("", gc);412}413414/**415* Constructs a new, initially invisible {@code Frame} object416* with the specified title.417* @param title the title to be displayed in the frame's border.418* A {@code null} value419* is treated as an empty string, "".420* @exception HeadlessException when421* {@code GraphicsEnvironment.isHeadless()} returns {@code true}422* @see java.awt.GraphicsEnvironment#isHeadless()423* @see java.awt.Component#setSize424* @see java.awt.Component#setVisible(boolean)425* @see java.awt.GraphicsConfiguration#getBounds426*/427public Frame(String title) throws HeadlessException {428init(title, null);429}430431/**432* Constructs a new, initially invisible {@code Frame} object433* with the specified title and a434* {@code GraphicsConfiguration}.435* @param title the title to be displayed in the frame's border.436* A {@code null} value437* is treated as an empty string, "".438* @param gc the {@code GraphicsConfiguration}439* of the target screen device. If {@code gc} is440* {@code null}, the system default441* {@code GraphicsConfiguration} is assumed.442* @exception IllegalArgumentException if {@code gc}443* is not from a screen device.444* @exception HeadlessException when445* {@code GraphicsEnvironment.isHeadless()} returns {@code true}446* @see java.awt.GraphicsEnvironment#isHeadless()447* @see java.awt.Component#setSize448* @see java.awt.Component#setVisible(boolean)449* @see java.awt.GraphicsConfiguration#getBounds450* @since 1.3451*/452public Frame(String title, GraphicsConfiguration gc) {453super(gc);454init(title, gc);455}456457private void init(String title, GraphicsConfiguration gc) {458this.title = title;459SunToolkit.checkAndSetPolicy(this);460}461462/**463* Construct a name for this component. Called by getName() when the464* name is null.465*/466String constructComponentName() {467synchronized (Frame.class) {468return base + nameCounter++;469}470}471472/**473* Makes this Frame displayable by connecting it to474* a native screen resource. Making a frame displayable will475* cause any of its children to be made displayable.476* This method is called internally by the toolkit and should477* not be called directly by programs.478* @see Component#isDisplayable479* @see #removeNotify480*/481public void addNotify() {482synchronized (getTreeLock()) {483if (peer == null) {484peer = getComponentFactory().createFrame(this);485}486FramePeer p = (FramePeer)peer;487MenuBar menuBar = this.menuBar;488if (menuBar != null) {489mbManagement = true;490menuBar.addNotify();491p.setMenuBar(menuBar);492}493p.setMaximizedBounds(maximizedBounds);494super.addNotify();495}496}497498/**499* Gets the title of the frame. The title is displayed in the500* frame's border.501* @return the title of this frame, or an empty string ("")502* if this frame doesn't have a title.503* @see #setTitle(String)504*/505public String getTitle() {506return title;507}508509/**510* Sets the title for this frame to the specified string.511* @param title the title to be displayed in the frame's border.512* A {@code null} value513* is treated as an empty string, "".514* @see #getTitle515*/516public void setTitle(String title) {517String oldTitle = this.title;518if (title == null) {519title = "";520}521522523synchronized(this) {524this.title = title;525FramePeer peer = (FramePeer)this.peer;526if (peer != null) {527peer.setTitle(title);528}529}530firePropertyChange("title", oldTitle, title);531}532533/**534* Returns the image to be displayed as the icon for this frame.535* <p>536* This method is obsolete and kept for backward compatibility537* only. Use {@link Window#getIconImages Window.getIconImages()} instead.538* <p>539* If a list of several images was specified as a Window's icon,540* this method will return the first item of the list.541*542* @return the icon image for this frame, or {@code null}543* if this frame doesn't have an icon image.544* @see #setIconImage(Image)545* @see Window#getIconImages()546* @see Window#setIconImages547*/548public Image getIconImage() {549java.util.List<Image> icons = this.icons;550if (icons != null) {551if (icons.size() > 0) {552return icons.get(0);553}554}555return null;556}557558/**559* {@inheritDoc}560*/561public void setIconImage(Image image) {562super.setIconImage(image);563}564565/**566* Gets the menu bar for this frame.567* @return the menu bar for this frame, or {@code null}568* if this frame doesn't have a menu bar.569* @see #setMenuBar(MenuBar)570*/571public MenuBar getMenuBar() {572return menuBar;573}574575/**576* Sets the menu bar for this frame to the specified menu bar.577* @param mb the menu bar being set.578* If this parameter is {@code null} then any579* existing menu bar on this frame is removed.580* @see #getMenuBar581*/582public void setMenuBar(MenuBar mb) {583synchronized (getTreeLock()) {584if (menuBar == mb) {585return;586}587if ((mb != null) && (mb.parent != null)) {588mb.parent.remove(mb);589}590if (menuBar != null) {591remove(menuBar);592}593menuBar = mb;594if (menuBar != null) {595menuBar.parent = this;596597FramePeer peer = (FramePeer)this.peer;598if (peer != null) {599mbManagement = true;600menuBar.addNotify();601invalidateIfValid();602peer.setMenuBar(menuBar);603}604}605}606}607608/**609* Indicates whether this frame is resizable by the user.610* By default, all frames are initially resizable.611* @return {@code true} if the user can resize this frame;612* {@code false} otherwise.613* @see java.awt.Frame#setResizable(boolean)614*/615public boolean isResizable() {616return resizable;617}618619/**620* Sets whether this frame is resizable by the user.621* @param resizable {@code true} if this frame is resizable;622* {@code false} otherwise.623* @see java.awt.Frame#isResizable624*/625public void setResizable(boolean resizable) {626boolean oldResizable = this.resizable;627boolean testvalid = false;628629synchronized (this) {630this.resizable = resizable;631FramePeer peer = (FramePeer)this.peer;632if (peer != null) {633peer.setResizable(resizable);634testvalid = true;635}636}637638// On some platforms, changing the resizable state affects639// the insets of the Frame. If we could, we'd call invalidate()640// from the peer, but we need to guarantee that we're not holding641// the Frame lock when we call invalidate().642if (testvalid) {643invalidateIfValid();644}645firePropertyChange("resizable", oldResizable, resizable);646}647648649/**650* Sets the state of this frame (obsolete).651* <p>652* In older versions of JDK a frame state could only be NORMAL or653* ICONIFIED. Since JDK 1.4 set of supported frame states is654* expanded and frame state is represented as a bitwise mask.655* <p>656* For compatibility with applications developed657* earlier this method still accepts658* {@code Frame.NORMAL} and659* {@code Frame.ICONIFIED} only. The iconic660* state of the frame is only changed, other aspects661* of frame state are not affected by this method. If662* the state passed to this method is neither {@code663* Frame.NORMAL} nor {@code Frame.ICONIFIED} the664* method performs no actions at all.665* <p>Note that if the state is not supported on a666* given platform, neither the state nor the return667* value of the {@link #getState} method will be668* changed. The application may determine whether a669* specific state is supported via the {@link670* java.awt.Toolkit#isFrameStateSupported} method.671* <p><b>If the frame is currently visible on the672* screen</b> (the {@link #isShowing} method returns673* {@code true}), the developer should examine the674* return value of the {@link675* java.awt.event.WindowEvent#getNewState} method of676* the {@code WindowEvent} received through the677* {@link java.awt.event.WindowStateListener} to678* determine that the state has actually been679* changed.680* <p><b>If the frame is not visible on the681* screen</b>, the events may or may not be682* generated. In this case the developer may assume683* that the state changes immediately after this684* method returns. Later, when the {@code685* setVisible(true)} method is invoked, the frame686* will attempt to apply this state. Receiving any687* {@link688* java.awt.event.WindowEvent#WINDOW_STATE_CHANGED}689* events is not guaranteed in this case also.690*691* @param state either {@code Frame.NORMAL} or692* {@code Frame.ICONIFIED}.693* @see #setExtendedState(int)694* @see java.awt.Window#addWindowStateListener695*/696public synchronized void setState(int state) {697int current = getExtendedState();698if (state == ICONIFIED && (current & ICONIFIED) == 0) {699setExtendedState(current | ICONIFIED);700}701else if (state == NORMAL && (current & ICONIFIED) != 0) {702setExtendedState(current & ~ICONIFIED);703}704}705706/**707* Sets the state of this frame. The state is708* represented as a bitwise mask.709* <ul>710* <li>{@code NORMAL}711* <br>Indicates that no state bits are set.712* <li>{@code ICONIFIED}713* <li>{@code MAXIMIZED_HORIZ}714* <li>{@code MAXIMIZED_VERT}715* <li>{@code MAXIMIZED_BOTH}716* <br>Concatenates {@code MAXIMIZED_HORIZ}717* and {@code MAXIMIZED_VERT}.718* </ul>719* <p>Note that if the state is not supported on a720* given platform, neither the state nor the return721* value of the {@link #getExtendedState} method will722* be changed. The application may determine whether723* a specific state is supported via the {@link724* java.awt.Toolkit#isFrameStateSupported} method.725* <p><b>If the frame is currently visible on the726* screen</b> (the {@link #isShowing} method returns727* {@code true}), the developer should examine the728* return value of the {@link729* java.awt.event.WindowEvent#getNewState} method of730* the {@code WindowEvent} received through the731* {@link java.awt.event.WindowStateListener} to732* determine that the state has actually been733* changed.734* <p><b>If the frame is not visible on the735* screen</b>, the events may or may not be736* generated. In this case the developer may assume737* that the state changes immediately after this738* method returns. Later, when the {@code739* setVisible(true)} method is invoked, the frame740* will attempt to apply this state. Receiving any741* {@link742* java.awt.event.WindowEvent#WINDOW_STATE_CHANGED}743* events is not guaranteed in this case also.744*745* @param state a bitwise mask of frame state constants746* @since 1.4747* @see java.awt.Window#addWindowStateListener748*/749public void setExtendedState(int state) {750if ( !isFrameStateSupported( state ) ) {751return;752}753synchronized (getObjectLock()) {754this.state = state;755}756// peer.setState must be called outside of object lock757// synchronization block to avoid possible deadlock758FramePeer peer = (FramePeer)this.peer;759if (peer != null) {760peer.setState(state);761}762}763private boolean isFrameStateSupported(int state) {764if( !getToolkit().isFrameStateSupported( state ) ) {765// * Toolkit.isFrameStateSupported returns always false766// on compound state even if all parts are supported;767// * if part of state is not supported, state is not supported;768// * MAXIMIZED_BOTH is not a compound state.769if( ((state & ICONIFIED) != 0) &&770!getToolkit().isFrameStateSupported( ICONIFIED )) {771return false;772}else {773state &= ~ICONIFIED;774}775return getToolkit().isFrameStateSupported( state );776}777return true;778}779780/**781* Gets the state of this frame (obsolete).782* <p>783* In older versions of JDK a frame state could only be NORMAL or784* ICONIFIED. Since JDK 1.4 set of supported frame states is785* expanded and frame state is represented as a bitwise mask.786* <p>787* For compatibility with old programs this method still returns788* {@code Frame.NORMAL} and {@code Frame.ICONIFIED} but789* it only reports the iconic state of the frame, other aspects of790* frame state are not reported by this method.791*792* @return {@code Frame.NORMAL} or {@code Frame.ICONIFIED}.793* @see #setState(int)794* @see #getExtendedState795*/796public synchronized int getState() {797return (getExtendedState() & ICONIFIED) != 0 ? ICONIFIED : NORMAL;798}799800801/**802* Gets the state of this frame. The state is803* represented as a bitwise mask.804* <ul>805* <li>{@code NORMAL}806* <br>Indicates that no state bits are set.807* <li>{@code ICONIFIED}808* <li>{@code MAXIMIZED_HORIZ}809* <li>{@code MAXIMIZED_VERT}810* <li>{@code MAXIMIZED_BOTH}811* <br>Concatenates {@code MAXIMIZED_HORIZ}812* and {@code MAXIMIZED_VERT}.813* </ul>814*815* @return a bitwise mask of frame state constants816* @see #setExtendedState(int)817* @since 1.4818*/819public int getExtendedState() {820synchronized (getObjectLock()) {821return state;822}823}824825static {826AWTAccessor.setFrameAccessor(827new AWTAccessor.FrameAccessor() {828public void setExtendedState(Frame frame, int state) {829synchronized(frame.getObjectLock()) {830frame.state = state;831}832}833public int getExtendedState(Frame frame) {834synchronized(frame.getObjectLock()) {835return frame.state;836}837}838public Rectangle getMaximizedBounds(Frame frame) {839synchronized(frame.getObjectLock()) {840return frame.maximizedBounds;841}842}843}844);845}846847/**848* Sets the maximized bounds for this frame.849* <p>850* When a frame is in maximized state the system supplies some851* defaults bounds. This method allows some or all of those852* system supplied values to be overridden.853* <p>854* If {@code bounds} is {@code null}, accept bounds855* supplied by the system. If non-{@code null} you can856* override some of the system supplied values while accepting857* others by setting those fields you want to accept from system858* to {@code Integer.MAX_VALUE}.859* <p>860* Note, the given maximized bounds are used as a hint for the native861* system, because the underlying platform may not support setting the862* location and/or size of the maximized windows. If that is the case, the863* provided values do not affect the appearance of the frame in the864* maximized state.865*866* @param bounds bounds for the maximized state867* @see #getMaximizedBounds()868* @since 1.4869*/870public void setMaximizedBounds(Rectangle bounds) {871synchronized(getObjectLock()) {872this.maximizedBounds = bounds;873}874FramePeer peer = (FramePeer)this.peer;875if (peer != null) {876peer.setMaximizedBounds(bounds);877}878}879880/**881* Gets maximized bounds for this frame.882* Some fields may contain {@code Integer.MAX_VALUE} to indicate883* that system supplied values for this field must be used.884*885* @return maximized bounds for this frame; may be {@code null}886* @see #setMaximizedBounds(Rectangle)887* @since 1.4888*/889public Rectangle getMaximizedBounds() {890synchronized(getObjectLock()) {891return maximizedBounds;892}893}894895896/**897* Disables or enables decorations for this frame.898* <p>899* This method can only be called while the frame is not displayable. To900* make this frame decorated, it must be opaque and have the default shape,901* otherwise the {@code IllegalComponentStateException} will be thrown.902* Refer to {@link Window#setShape}, {@link Window#setOpacity} and {@link903* Window#setBackground} for details904*905* @param undecorated {@code true} if no frame decorations are to be906* enabled; {@code false} if frame decorations are to be enabled907*908* @throws IllegalComponentStateException if the frame is displayable909* @throws IllegalComponentStateException if {@code undecorated} is910* {@code false}, and this frame does not have the default shape911* @throws IllegalComponentStateException if {@code undecorated} is912* {@code false}, and this frame opacity is less than {@code 1.0f}913* @throws IllegalComponentStateException if {@code undecorated} is914* {@code false}, and the alpha value of this frame background915* color is less than {@code 1.0f}916*917* @see #isUndecorated918* @see Component#isDisplayable919* @see Window#getShape920* @see Window#getOpacity921* @see Window#getBackground922* @see javax.swing.JFrame#setDefaultLookAndFeelDecorated(boolean)923*924* @since 1.4925*/926public void setUndecorated(boolean undecorated) {927/* Make sure we don't run in the middle of peer creation.*/928synchronized (getTreeLock()) {929if (isDisplayable()) {930throw new IllegalComponentStateException("The frame is displayable.");931}932if (!undecorated) {933if (getOpacity() < 1.0f) {934throw new IllegalComponentStateException("The frame is not opaque");935}936if (getShape() != null) {937throw new IllegalComponentStateException("The frame does not have a default shape");938}939Color bg = getBackground();940if ((bg != null) && (bg.getAlpha() < 255)) {941throw new IllegalComponentStateException("The frame background color is not opaque");942}943}944this.undecorated = undecorated;945}946}947948/**949* Indicates whether this frame is undecorated.950* By default, all frames are initially decorated.951* @return {@code true} if frame is undecorated;952* {@code false} otherwise.953* @see java.awt.Frame#setUndecorated(boolean)954* @since 1.4955*/956public boolean isUndecorated() {957return undecorated;958}959960/**961* {@inheritDoc}962*/963@Override964public void setOpacity(float opacity) {965synchronized (getTreeLock()) {966if ((opacity < 1.0f) && !isUndecorated()) {967throw new IllegalComponentStateException("The frame is decorated");968}969super.setOpacity(opacity);970}971}972973/**974* {@inheritDoc}975*/976@Override977public void setShape(Shape shape) {978synchronized (getTreeLock()) {979if ((shape != null) && !isUndecorated()) {980throw new IllegalComponentStateException("The frame is decorated");981}982super.setShape(shape);983}984}985986/**987* {@inheritDoc}988*/989@Override990public void setBackground(Color bgColor) {991synchronized (getTreeLock()) {992if ((bgColor != null) && (bgColor.getAlpha() < 255) && !isUndecorated()) {993throw new IllegalComponentStateException("The frame is decorated");994}995super.setBackground(bgColor);996}997}998999/**1000* Removes the specified menu bar from this frame.1001* @param m the menu component to remove.1002* If {@code m} is {@code null}, then1003* no action is taken1004*/1005public void remove(MenuComponent m) {1006if (m == null) {1007return;1008}1009synchronized (getTreeLock()) {1010if (m == menuBar) {1011menuBar = null;1012FramePeer peer = (FramePeer)this.peer;1013if (peer != null) {1014mbManagement = true;1015invalidateIfValid();1016peer.setMenuBar(null);1017m.removeNotify();1018}1019m.parent = null;1020} else {1021super.remove(m);1022}1023}1024}10251026/**1027* Makes this Frame undisplayable by removing its connection1028* to its native screen resource. Making a Frame undisplayable1029* will cause any of its children to be made undisplayable.1030* This method is called by the toolkit internally and should1031* not be called directly by programs.1032* @see Component#isDisplayable1033* @see #addNotify1034*/1035public void removeNotify() {1036synchronized (getTreeLock()) {1037FramePeer peer = (FramePeer)this.peer;1038if (peer != null) {1039// get the latest Frame state before disposing1040getState();10411042if (menuBar != null) {1043mbManagement = true;1044peer.setMenuBar(null);1045menuBar.removeNotify();1046}1047}1048super.removeNotify();1049}1050}10511052void postProcessKeyEvent(KeyEvent e) {1053if (menuBar != null && menuBar.handleShortcut(e)) {1054e.consume();1055return;1056}1057super.postProcessKeyEvent(e);1058}10591060/**1061* Returns a string representing the state of this {@code Frame}.1062* This method is intended to be used only for debugging purposes, and the1063* content and format of the returned string may vary between1064* implementations. The returned string may be empty but may not be1065* {@code null}.1066*1067* @return the parameter string of this frame1068*/1069protected String paramString() {1070String str = super.paramString();1071if (title != null) {1072str += ",title=" + title;1073}1074if (resizable) {1075str += ",resizable";1076}1077int state = getExtendedState();1078if (state == NORMAL) {1079str += ",normal";1080}1081else {1082if ((state & ICONIFIED) != 0) {1083str += ",iconified";1084}1085if ((state & MAXIMIZED_BOTH) == MAXIMIZED_BOTH) {1086str += ",maximized";1087}1088else if ((state & MAXIMIZED_HORIZ) != 0) {1089str += ",maximized_horiz";1090}1091else if ((state & MAXIMIZED_VERT) != 0) {1092str += ",maximized_vert";1093}1094}1095return str;1096}10971098/**1099* Sets the cursor for this frame to the specified type.1100*1101* @param cursorType the cursor type1102* @deprecated As of JDK version 1.1,1103* replaced by {@code Component.setCursor(Cursor)}.1104*/1105@Deprecated1106public void setCursor(int cursorType) {1107if (cursorType < DEFAULT_CURSOR || cursorType > MOVE_CURSOR) {1108throw new IllegalArgumentException("illegal cursor type");1109}1110setCursor(Cursor.getPredefinedCursor(cursorType));1111}11121113/**1114* @deprecated As of JDK version 1.1,1115* replaced by {@code Component.getCursor()}.1116* @return the cursor type for this frame1117*/1118@Deprecated1119public int getCursorType() {1120return (getCursor().getType());1121}11221123/**1124* Returns an array of all {@code Frame}s created by this application.1125* If called from an applet, the array includes only the {@code Frame}s1126* accessible by that applet.1127* <p>1128* <b>Warning:</b> this method may return system created frames, such1129* as a shared, hidden frame which is used by Swing. Applications1130* should not assume the existence of these frames, nor should an1131* application assume anything about these frames such as component1132* positions, {@code LayoutManager}s or serialization.1133* <p>1134* <b>Note</b>: To obtain a list of all ownerless windows, including1135* ownerless {@code Dialog}s (introduced in release 1.6), use {@link1136* Window#getOwnerlessWindows Window.getOwnerlessWindows}.1137*1138* @return the array of all {@code Frame}s created by this application1139*1140* @see Window#getWindows()1141* @see Window#getOwnerlessWindows1142*1143* @since 1.21144*/1145public static Frame[] getFrames() {1146Window[] allWindows = Window.getWindows();11471148int frameCount = 0;1149for (Window w : allWindows) {1150if (w instanceof Frame) {1151frameCount++;1152}1153}11541155Frame[] frames = new Frame[frameCount];1156int c = 0;1157for (Window w : allWindows) {1158if (w instanceof Frame) {1159frames[c++] = (Frame)w;1160}1161}11621163return frames;1164}11651166/* Serialization support. If there's a MenuBar we restore1167* its (transient) parent field here. Likewise for top level1168* windows that are "owned" by this frame.1169*/11701171/**1172* {@code Frame}'s Serialized Data Version.1173*1174* @serial1175*/1176private int frameSerializedDataVersion = 1;11771178/**1179* Writes default serializable fields to stream. Writes1180* an optional serializable icon {@code Image}, which is1181* available as of 1.4.1182*1183* @param s the {@code ObjectOutputStream} to write1184* @throws IOException if an I/O error occurs1185* @serialData an optional icon {@code Image}1186* @see java.awt.Image1187* @see #getIconImage1188* @see #setIconImage(Image)1189* @see #readObject(ObjectInputStream)1190*/1191@Serial1192private void writeObject(ObjectOutputStream s)1193throws IOException1194{1195s.defaultWriteObject();1196if (icons != null && icons.size() > 0) {1197Image icon1 = icons.get(0);1198if (icon1 instanceof Serializable) {1199s.writeObject(icon1);1200return;1201}1202}1203s.writeObject(null);1204}12051206/**1207* Reads the {@code ObjectInputStream}. Tries1208* to read an icon {@code Image}, which is optional1209* data available as of 1.4. If an icon {@code Image}1210* is not available, but anything other than an EOF1211* is detected, an {@code OptionalDataException}1212* will be thrown.1213* Unrecognized keys or values will be ignored.1214*1215* @param s the {@code ObjectInputStream} to read1216* @throws ClassNotFoundException if the class of a serialized object could1217* not be found1218* @throws java.io.OptionalDataException if an icon {@code Image} is not1219* available, but anything other than an EOF is detected1220* @throws HeadlessException if {@code GraphicsEnvironment.isHeadless()}1221* returns {@code true}1222* @see java.awt.GraphicsEnvironment#isHeadless()1223* @see java.awt.Image1224* @see #getIconImage1225* @see #setIconImage(Image)1226* @see #writeObject(ObjectOutputStream)1227*/1228@Serial1229private void readObject(ObjectInputStream s)1230throws ClassNotFoundException, IOException, HeadlessException1231{1232// HeadlessException is thrown by Window's readObject1233s.defaultReadObject();1234try {1235Image icon = (Image) s.readObject();1236if (icons == null) {1237icons = new ArrayList<Image>();1238icons.add(icon);1239}1240} catch (java.io.OptionalDataException e) {1241// pre-1.4 instances will not have this optional data.1242// 1.6 and later instances serialize icons in the Window class1243// e.eof will be true to indicate that there is no more1244// data available for this object.12451246// If e.eof is not true, throw the exception as it1247// might have been caused by unrelated reasons.1248if (!e.eof) {1249throw (e);1250}1251}12521253if (menuBar != null)1254menuBar.parent = this;12551256// Ensure 1.1 serialized Frames can read & hook-up1257// owned windows properly1258//1259if (ownedWindows != null) {1260for (int i = 0; i < ownedWindows.size(); i++) {1261connectOwnedWindow(ownedWindows.elementAt(i));1262}1263ownedWindows = null;1264}1265}12661267/**1268* Initialize JNI field and method IDs1269*/1270private static native void initIDs();12711272/*1273* --- Accessibility Support ---1274*1275*/12761277/**1278* Gets the AccessibleContext associated with this Frame.1279* For frames, the AccessibleContext takes the form of an1280* AccessibleAWTFrame.1281* A new AccessibleAWTFrame instance is created if necessary.1282*1283* @return an AccessibleAWTFrame that serves as the1284* AccessibleContext of this Frame1285* @since 1.31286*/1287public AccessibleContext getAccessibleContext() {1288if (accessibleContext == null) {1289accessibleContext = new AccessibleAWTFrame();1290}1291return accessibleContext;1292}12931294/**1295* This class implements accessibility support for the1296* {@code Frame} class. It provides an implementation of the1297* Java Accessibility API appropriate to frame user-interface elements.1298* @since 1.31299*/1300protected class AccessibleAWTFrame extends AccessibleAWTWindow1301{1302/**1303* Use serialVersionUID from JDK 1.3 for interoperability.1304*/1305@Serial1306private static final long serialVersionUID = -6172960752956030250L;13071308/**1309* Constructs an {@code AccessibleAWTFrame}.1310*/1311protected AccessibleAWTFrame() {}13121313/**1314* Get the role of this object.1315*1316* @return an instance of AccessibleRole describing the role of the1317* object1318* @see AccessibleRole1319*/1320public AccessibleRole getAccessibleRole() {1321return AccessibleRole.FRAME;1322}13231324/**1325* Get the state of this object.1326*1327* @return an instance of AccessibleStateSet containing the current1328* state set of the object1329* @see AccessibleState1330*/1331public AccessibleStateSet getAccessibleStateSet() {1332AccessibleStateSet states = super.getAccessibleStateSet();1333if (getFocusOwner() != null) {1334states.add(AccessibleState.ACTIVE);1335}1336if (isResizable()) {1337states.add(AccessibleState.RESIZABLE);1338}1339return states;1340}134113421343} // inner class AccessibleAWTFrame13441345}134613471348