Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/Frame.java
38829 views
/*1* Copyright (c) 1995, 2013, 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*/24package java.awt;2526import java.awt.peer.FramePeer;27import java.awt.event.*;28import java.util.ArrayList;29import java.util.Arrays;30import java.util.List;31import java.util.Vector;32import java.io.Serializable;33import java.io.ObjectOutputStream;34import java.io.ObjectInputStream;35import java.io.IOException;36import sun.awt.AppContext;37import sun.awt.SunToolkit;38import sun.awt.AWTAccessor;39import java.lang.ref.WeakReference;40import javax.accessibility.*;4142/**43* A <code>Frame</code> is a top-level window with a title and a border.44* <p>45* The size of the frame includes any area designated for the46* border. The dimensions of the border area may be obtained47* using the <code>getInsets</code> method, however, since48* these dimensions are platform-dependent, a valid insets49* value cannot be obtained until the frame is made displayable50* by either calling <code>pack</code> or <code>show</code>.51* Since the border area is included in the overall size of the52* frame, the border effectively obscures a portion of the frame,53* constraining the area available for rendering and/or displaying54* subcomponents to the rectangle which has an upper-left corner55* location of <code>(insets.left, insets.top)</code>, and has a size of56* <code>width - (insets.left + insets.right)</code> by57* <code>height - (insets.top + insets.bottom)</code>.58* <p>59* The default layout for a frame is <code>BorderLayout</code>.60* <p>61* A frame may have its native decorations (i.e. <code>Frame</code>62* and <code>Titlebar</code>) turned off63* with <code>setUndecorated</code>. This can only be done while the frame64* is not {@link Component#isDisplayable() displayable}.65* <p>66* In a multi-screen environment, you can create a <code>Frame</code>67* on a different screen device by constructing the <code>Frame</code>68* with {@link #Frame(GraphicsConfiguration)} or69* {@link #Frame(String title, GraphicsConfiguration)}. The70* <code>GraphicsConfiguration</code> object is one of the71* <code>GraphicsConfiguration</code> objects of the target screen72* device.73* <p>74* In a virtual device multi-screen environment in which the desktop75* area could span multiple physical screen devices, the bounds of all76* configurations are relative to the virtual-coordinate system. The77* origin of the virtual-coordinate system is at the upper left-hand78* corner of the primary physical screen. Depending on the location79* of the primary screen in the virtual device, negative coordinates80* are possible, as shown in the following figure.81* <p>82* <img src="doc-files/MultiScreen.gif"83* alt="Diagram of virtual device encompassing three physical screens and one primary physical screen. The primary physical screen84* shows (0,0) coords while a different physical screen shows (-80,-100) coords."85* style="float:center; margin: 7px 10px;">86* <p>87* In such an environment, when calling <code>setLocation</code>,88* you must pass a virtual coordinate to this method. Similarly,89* calling <code>getLocationOnScreen</code> on a <code>Frame</code>90* returns virtual device coordinates. Call the <code>getBounds</code>91* method of a <code>GraphicsConfiguration</code> to find its origin in92* the virtual coordinate system.93* <p>94* The following code sets the95* location of the <code>Frame</code> at (10, 10) relative96* to the origin of the physical screen of the corresponding97* <code>GraphicsConfiguration</code>. If the bounds of the98* <code>GraphicsConfiguration</code> is not taken into account, the99* <code>Frame</code> location would be set at (10, 10) relative to the100* virtual-coordinate system and would appear on the primary physical101* screen, which might be different from the physical screen of the102* specified <code>GraphicsConfiguration</code>.103*104* <pre>105* Frame f = new Frame(GraphicsConfiguration gc);106* Rectangle bounds = gc.getBounds();107* f.setLocation(10 + bounds.x, 10 + bounds.y);108* </pre>109*110* <p>111* Frames are capable of generating the following types of112* <code>WindowEvent</code>s:113* <ul>114* <li><code>WINDOW_OPENED</code>115* <li><code>WINDOW_CLOSING</code>:116* <br>If the program doesn't117* explicitly hide or dispose the window while processing118* this event, the window close operation is canceled.119* <li><code>WINDOW_CLOSED</code>120* <li><code>WINDOW_ICONIFIED</code>121* <li><code>WINDOW_DEICONIFIED</code>122* <li><code>WINDOW_ACTIVATED</code>123* <li><code>WINDOW_DEACTIVATED</code>124* <li><code>WINDOW_GAINED_FOCUS</code>125* <li><code>WINDOW_LOST_FOCUS</code>126* <li><code>WINDOW_STATE_CHANGED</code>127* </ul>128*129* @author Sami Shaio130* @see WindowEvent131* @see Window#addWindowListener132* @since JDK1.0133*/134public class Frame extends Window implements MenuContainer {135136/* Note: These are being obsoleted; programs should use the Cursor class137* variables going forward. See Cursor and Component.setCursor.138*/139140/**141* @deprecated replaced by <code>Cursor.DEFAULT_CURSOR</code>.142*/143@Deprecated144public static final int DEFAULT_CURSOR = Cursor.DEFAULT_CURSOR;145146147/**148* @deprecated replaced by <code>Cursor.CROSSHAIR_CURSOR</code>.149*/150@Deprecated151public static final int CROSSHAIR_CURSOR = Cursor.CROSSHAIR_CURSOR;152153/**154* @deprecated replaced by <code>Cursor.TEXT_CURSOR</code>.155*/156@Deprecated157public static final int TEXT_CURSOR = Cursor.TEXT_CURSOR;158159/**160* @deprecated replaced by <code>Cursor.WAIT_CURSOR</code>.161*/162@Deprecated163public static final int WAIT_CURSOR = Cursor.WAIT_CURSOR;164165/**166* @deprecated replaced by <code>Cursor.SW_RESIZE_CURSOR</code>.167*/168@Deprecated169public static final int SW_RESIZE_CURSOR = Cursor.SW_RESIZE_CURSOR;170171/**172* @deprecated replaced by <code>Cursor.SE_RESIZE_CURSOR</code>.173*/174@Deprecated175public static final int SE_RESIZE_CURSOR = Cursor.SE_RESIZE_CURSOR;176177/**178* @deprecated replaced by <code>Cursor.NW_RESIZE_CURSOR</code>.179*/180@Deprecated181public static final int NW_RESIZE_CURSOR = Cursor.NW_RESIZE_CURSOR;182183/**184* @deprecated replaced by <code>Cursor.NE_RESIZE_CURSOR</code>.185*/186@Deprecated187public static final int NE_RESIZE_CURSOR = Cursor.NE_RESIZE_CURSOR;188189/**190* @deprecated replaced by <code>Cursor.N_RESIZE_CURSOR</code>.191*/192@Deprecated193public static final int N_RESIZE_CURSOR = Cursor.N_RESIZE_CURSOR;194195/**196* @deprecated replaced by <code>Cursor.S_RESIZE_CURSOR</code>.197*/198@Deprecated199public static final int S_RESIZE_CURSOR = Cursor.S_RESIZE_CURSOR;200201/**202* @deprecated replaced by <code>Cursor.W_RESIZE_CURSOR</code>.203*/204@Deprecated205public static final int W_RESIZE_CURSOR = Cursor.W_RESIZE_CURSOR;206207/**208* @deprecated replaced by <code>Cursor.E_RESIZE_CURSOR</code>.209*/210@Deprecated211public static final int E_RESIZE_CURSOR = Cursor.E_RESIZE_CURSOR;212213/**214* @deprecated replaced by <code>Cursor.HAND_CURSOR</code>.215*/216@Deprecated217public static final int HAND_CURSOR = Cursor.HAND_CURSOR;218219/**220* @deprecated replaced by <code>Cursor.MOVE_CURSOR</code>.221*/222@Deprecated223public static final int MOVE_CURSOR = Cursor.MOVE_CURSOR;224225226/**227* Frame is in the "normal" state. This symbolic constant names a228* frame state with all state bits cleared.229* @see #setExtendedState(int)230* @see #getExtendedState231*/232public static final int NORMAL = 0;233234/**235* This state bit indicates that frame is iconified.236* @see #setExtendedState(int)237* @see #getExtendedState238*/239public static final int ICONIFIED = 1;240241/**242* This state bit indicates that frame is maximized in the243* horizontal direction.244* @see #setExtendedState(int)245* @see #getExtendedState246* @since 1.4247*/248public static final int MAXIMIZED_HORIZ = 2;249250/**251* This state bit indicates that frame is maximized in the252* vertical direction.253* @see #setExtendedState(int)254* @see #getExtendedState255* @since 1.4256*/257public static final int MAXIMIZED_VERT = 4;258259/**260* This state bit mask indicates that frame is fully maximized261* (that is both horizontally and vertically). It is just a262* convenience alias for263* <code>MAXIMIZED_VERT | MAXIMIZED_HORIZ</code>.264*265* <p>Note that the correct test for frame being fully maximized is266* <pre>267* (state & Frame.MAXIMIZED_BOTH) == Frame.MAXIMIZED_BOTH268* </pre>269*270* <p>To test is frame is maximized in <em>some</em> direction use271* <pre>272* (state & Frame.MAXIMIZED_BOTH) != 0273* </pre>274*275* @see #setExtendedState(int)276* @see #getExtendedState277* @since 1.4278*/279public static final int MAXIMIZED_BOTH = MAXIMIZED_VERT | MAXIMIZED_HORIZ;280281/**282* Maximized bounds for this frame.283* @see #setMaximizedBounds(Rectangle)284* @see #getMaximizedBounds285* @serial286* @since 1.4287*/288Rectangle maximizedBounds;289290291/**292* This is the title of the frame. It can be changed293* at any time. <code>title</code> can be null and if294* this is the case the <code>title</code> = "".295*296* @serial297* @see #getTitle298* @see #setTitle(String)299*/300String title = "Untitled";301302/**303* The frames menubar. If <code>menuBar</code> = null304* the frame will not have a menubar.305*306* @serial307* @see #getMenuBar308* @see #setMenuBar(MenuBar)309*/310MenuBar menuBar;311312/**313* This field indicates whether the frame is resizable.314* This property can be changed at any time.315* <code>resizable</code> will be true if the frame is316* resizable, otherwise it will be false.317*318* @serial319* @see #isResizable()320*/321boolean resizable = true;322323/**324* This field indicates whether the frame is undecorated.325* This property can only be changed while the frame is not displayable.326* <code>undecorated</code> will be true if the frame is327* undecorated, otherwise it will be false.328*329* @serial330* @see #setUndecorated(boolean)331* @see #isUndecorated()332* @see Component#isDisplayable()333* @since 1.4334*/335boolean undecorated = false;336337/**338* <code>mbManagement</code> is only used by the Motif implementation.339*340* @serial341*/342boolean mbManagement = false; /* used only by the Motif impl. */343344// XXX: uwe: abuse old field for now345// will need to take care of serialization346private int state = NORMAL;347348/*349* The Windows owned by the Frame.350* Note: in 1.2 this has been superceded by Window.ownedWindowList351*352* @serial353* @see java.awt.Window#ownedWindowList354*/355Vector<Window> ownedWindows;356357private static final String base = "frame";358private static int nameCounter = 0;359360/*361* JDK 1.1 serialVersionUID362*/363private static final long serialVersionUID = 2673458971256075116L;364365static {366/* ensure that the necessary native libraries are loaded */367Toolkit.loadLibraries();368if (!GraphicsEnvironment.isHeadless()) {369initIDs();370}371}372373/**374* Constructs a new instance of <code>Frame</code> that is375* initially invisible. The title of the <code>Frame</code>376* is empty.377* @exception HeadlessException when378* <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>379* @see java.awt.GraphicsEnvironment#isHeadless()380* @see Component#setSize381* @see Component#setVisible(boolean)382*/383public Frame() throws HeadlessException {384this("");385}386387/**388* Constructs a new, initially invisible {@code Frame} with the389* specified {@code GraphicsConfiguration}.390*391* @param gc the <code>GraphicsConfiguration</code>392* of the target screen device. If <code>gc</code>393* is <code>null</code>, the system default394* <code>GraphicsConfiguration</code> is assumed.395* @exception IllegalArgumentException if396* <code>gc</code> is not from a screen device.397* @exception HeadlessException when398* <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>399* @see java.awt.GraphicsEnvironment#isHeadless()400* @since 1.3401*/402public Frame(GraphicsConfiguration gc) {403this("", gc);404}405406/**407* Constructs a new, initially invisible <code>Frame</code> object408* with the specified title.409* @param title the title to be displayed in the frame's border.410* A <code>null</code> value411* is treated as an empty string, "".412* @exception HeadlessException when413* <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>414* @see java.awt.GraphicsEnvironment#isHeadless()415* @see java.awt.Component#setSize416* @see java.awt.Component#setVisible(boolean)417* @see java.awt.GraphicsConfiguration#getBounds418*/419public Frame(String title) throws HeadlessException {420init(title, null);421}422423/**424* Constructs a new, initially invisible <code>Frame</code> object425* with the specified title and a426* <code>GraphicsConfiguration</code>.427* @param title the title to be displayed in the frame's border.428* A <code>null</code> value429* is treated as an empty string, "".430* @param gc the <code>GraphicsConfiguration</code>431* of the target screen device. If <code>gc</code> is432* <code>null</code>, the system default433* <code>GraphicsConfiguration</code> is assumed.434* @exception IllegalArgumentException if <code>gc</code>435* is not from a screen device.436* @exception HeadlessException when437* <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>438* @see java.awt.GraphicsEnvironment#isHeadless()439* @see java.awt.Component#setSize440* @see java.awt.Component#setVisible(boolean)441* @see java.awt.GraphicsConfiguration#getBounds442* @since 1.3443*/444public Frame(String title, GraphicsConfiguration gc) {445super(gc);446init(title, gc);447}448449private void init(String title, GraphicsConfiguration gc) {450this.title = title;451SunToolkit.checkAndSetPolicy(this);452}453454/**455* Construct a name for this component. Called by getName() when the456* name is null.457*/458String constructComponentName() {459synchronized (Frame.class) {460return base + nameCounter++;461}462}463464/**465* Makes this Frame displayable by connecting it to466* a native screen resource. Making a frame displayable will467* cause any of its children to be made displayable.468* This method is called internally by the toolkit and should469* not be called directly by programs.470* @see Component#isDisplayable471* @see #removeNotify472*/473public void addNotify() {474synchronized (getTreeLock()) {475if (peer == null) {476peer = getToolkit().createFrame(this);477}478FramePeer p = (FramePeer)peer;479MenuBar menuBar = this.menuBar;480if (menuBar != null) {481mbManagement = true;482menuBar.addNotify();483p.setMenuBar(menuBar);484}485p.setMaximizedBounds(maximizedBounds);486super.addNotify();487}488}489490/**491* Gets the title of the frame. The title is displayed in the492* frame's border.493* @return the title of this frame, or an empty string ("")494* if this frame doesn't have a title.495* @see #setTitle(String)496*/497public String getTitle() {498return title;499}500501/**502* Sets the title for this frame to the specified string.503* @param title the title to be displayed in the frame's border.504* A <code>null</code> value505* is treated as an empty string, "".506* @see #getTitle507*/508public void setTitle(String title) {509String oldTitle = this.title;510if (title == null) {511title = "";512}513514515synchronized(this) {516this.title = title;517FramePeer peer = (FramePeer)this.peer;518if (peer != null) {519peer.setTitle(title);520}521}522firePropertyChange("title", oldTitle, title);523}524525/**526* Returns the image to be displayed as the icon for this frame.527* <p>528* This method is obsolete and kept for backward compatibility529* only. Use {@link Window#getIconImages Window.getIconImages()} instead.530* <p>531* If a list of several images was specified as a Window's icon,532* this method will return the first item of the list.533*534* @return the icon image for this frame, or <code>null</code>535* if this frame doesn't have an icon image.536* @see #setIconImage(Image)537* @see Window#getIconImages()538* @see Window#setIconImages539*/540public Image getIconImage() {541java.util.List<Image> icons = this.icons;542if (icons != null) {543if (icons.size() > 0) {544return icons.get(0);545}546}547return null;548}549550/**551* {@inheritDoc}552*/553public void setIconImage(Image image) {554super.setIconImage(image);555}556557/**558* Gets the menu bar for this frame.559* @return the menu bar for this frame, or <code>null</code>560* if this frame doesn't have a menu bar.561* @see #setMenuBar(MenuBar)562*/563public MenuBar getMenuBar() {564return menuBar;565}566567/**568* Sets the menu bar for this frame to the specified menu bar.569* @param mb the menu bar being set.570* If this parameter is <code>null</code> then any571* existing menu bar on this frame is removed.572* @see #getMenuBar573*/574public void setMenuBar(MenuBar mb) {575synchronized (getTreeLock()) {576if (menuBar == mb) {577return;578}579if ((mb != null) && (mb.parent != null)) {580mb.parent.remove(mb);581}582if (menuBar != null) {583remove(menuBar);584}585menuBar = mb;586if (menuBar != null) {587menuBar.parent = this;588589FramePeer peer = (FramePeer)this.peer;590if (peer != null) {591mbManagement = true;592menuBar.addNotify();593invalidateIfValid();594peer.setMenuBar(menuBar);595}596}597}598}599600/**601* Indicates whether this frame is resizable by the user.602* By default, all frames are initially resizable.603* @return <code>true</code> if the user can resize this frame;604* <code>false</code> otherwise.605* @see java.awt.Frame#setResizable(boolean)606*/607public boolean isResizable() {608return resizable;609}610611/**612* Sets whether this frame is resizable by the user.613* @param resizable <code>true</code> if this frame is resizable;614* <code>false</code> otherwise.615* @see java.awt.Frame#isResizable616*/617public void setResizable(boolean resizable) {618boolean oldResizable = this.resizable;619boolean testvalid = false;620621synchronized (this) {622this.resizable = resizable;623FramePeer peer = (FramePeer)this.peer;624if (peer != null) {625peer.setResizable(resizable);626testvalid = true;627}628}629630// On some platforms, changing the resizable state affects631// the insets of the Frame. If we could, we'd call invalidate()632// from the peer, but we need to guarantee that we're not holding633// the Frame lock when we call invalidate().634if (testvalid) {635invalidateIfValid();636}637firePropertyChange("resizable", oldResizable, resizable);638}639640641/**642* Sets the state of this frame (obsolete).643* <p>644* In older versions of JDK a frame state could only be NORMAL or645* ICONIFIED. Since JDK 1.4 set of supported frame states is646* expanded and frame state is represented as a bitwise mask.647* <p>648* For compatibility with applications developed649* earlier this method still accepts650* {@code Frame.NORMAL} and651* {@code Frame.ICONIFIED} only. The iconic652* state of the frame is only changed, other aspects653* of frame state are not affected by this method. If654* the state passed to this method is neither {@code655* Frame.NORMAL} nor {@code Frame.ICONIFIED} the656* method performs no actions at all.657* <p>Note that if the state is not supported on a658* given platform, neither the state nor the return659* value of the {@link #getState} method will be660* changed. The application may determine whether a661* specific state is supported via the {@link662* java.awt.Toolkit#isFrameStateSupported} method.663* <p><b>If the frame is currently visible on the664* screen</b> (the {@link #isShowing} method returns665* {@code true}), the developer should examine the666* return value of the {@link667* java.awt.event.WindowEvent#getNewState} method of668* the {@code WindowEvent} received through the669* {@link java.awt.event.WindowStateListener} to670* determine that the state has actually been671* changed.672* <p><b>If the frame is not visible on the673* screen</b>, the events may or may not be674* generated. In this case the developer may assume675* that the state changes immediately after this676* method returns. Later, when the {@code677* setVisible(true)} method is invoked, the frame678* will attempt to apply this state. Receiving any679* {@link680* java.awt.event.WindowEvent#WINDOW_STATE_CHANGED}681* events is not guaranteed in this case also.682*683* @param state either <code>Frame.NORMAL</code> or684* <code>Frame.ICONIFIED</code>.685* @see #setExtendedState(int)686* @see java.awt.Window#addWindowStateListener687*/688public synchronized void setState(int state) {689int current = getExtendedState();690if (state == ICONIFIED && (current & ICONIFIED) == 0) {691setExtendedState(current | ICONIFIED);692}693else if (state == NORMAL && (current & ICONIFIED) != 0) {694setExtendedState(current & ~ICONIFIED);695}696}697698/**699* Sets the state of this frame. The state is700* represented as a bitwise mask.701* <ul>702* <li><code>NORMAL</code>703* <br>Indicates that no state bits are set.704* <li><code>ICONIFIED</code>705* <li><code>MAXIMIZED_HORIZ</code>706* <li><code>MAXIMIZED_VERT</code>707* <li><code>MAXIMIZED_BOTH</code>708* <br>Concatenates <code>MAXIMIZED_HORIZ</code>709* and <code>MAXIMIZED_VERT</code>.710* </ul>711* <p>Note that if the state is not supported on a712* given platform, neither the state nor the return713* value of the {@link #getExtendedState} method will714* be changed. The application may determine whether715* a specific state is supported via the {@link716* java.awt.Toolkit#isFrameStateSupported} method.717* <p><b>If the frame is currently visible on the718* screen</b> (the {@link #isShowing} method returns719* {@code true}), the developer should examine the720* return value of the {@link721* java.awt.event.WindowEvent#getNewState} method of722* the {@code WindowEvent} received through the723* {@link java.awt.event.WindowStateListener} to724* determine that the state has actually been725* changed.726* <p><b>If the frame is not visible on the727* screen</b>, the events may or may not be728* generated. In this case the developer may assume729* that the state changes immediately after this730* method returns. Later, when the {@code731* setVisible(true)} method is invoked, the frame732* will attempt to apply this state. Receiving any733* {@link734* java.awt.event.WindowEvent#WINDOW_STATE_CHANGED}735* events is not guaranteed in this case also.736*737* @param state a bitwise mask of frame state constants738* @since 1.4739* @see java.awt.Window#addWindowStateListener740*/741public void setExtendedState(int state) {742if ( !isFrameStateSupported( state ) ) {743return;744}745synchronized (getObjectLock()) {746this.state = state;747}748// peer.setState must be called outside of object lock749// synchronization block to avoid possible deadlock750FramePeer peer = (FramePeer)this.peer;751if (peer != null) {752peer.setState(state);753}754}755private boolean isFrameStateSupported(int state) {756if( !getToolkit().isFrameStateSupported( state ) ) {757// * Toolkit.isFrameStateSupported returns always false758// on compound state even if all parts are supported;759// * if part of state is not supported, state is not supported;760// * MAXIMIZED_BOTH is not a compound state.761if( ((state & ICONIFIED) != 0) &&762!getToolkit().isFrameStateSupported( ICONIFIED )) {763return false;764}else {765state &= ~ICONIFIED;766}767return getToolkit().isFrameStateSupported( state );768}769return true;770}771772/**773* Gets the state of this frame (obsolete).774* <p>775* In older versions of JDK a frame state could only be NORMAL or776* ICONIFIED. Since JDK 1.4 set of supported frame states is777* expanded and frame state is represented as a bitwise mask.778* <p>779* For compatibility with old programs this method still returns780* <code>Frame.NORMAL</code> and <code>Frame.ICONIFIED</code> but781* it only reports the iconic state of the frame, other aspects of782* frame state are not reported by this method.783*784* @return <code>Frame.NORMAL</code> or <code>Frame.ICONIFIED</code>.785* @see #setState(int)786* @see #getExtendedState787*/788public synchronized int getState() {789return (getExtendedState() & ICONIFIED) != 0 ? ICONIFIED : NORMAL;790}791792793/**794* Gets the state of this frame. The state is795* represented as a bitwise mask.796* <ul>797* <li><code>NORMAL</code>798* <br>Indicates that no state bits are set.799* <li><code>ICONIFIED</code>800* <li><code>MAXIMIZED_HORIZ</code>801* <li><code>MAXIMIZED_VERT</code>802* <li><code>MAXIMIZED_BOTH</code>803* <br>Concatenates <code>MAXIMIZED_HORIZ</code>804* and <code>MAXIMIZED_VERT</code>.805* </ul>806*807* @return a bitwise mask of frame state constants808* @see #setExtendedState(int)809* @since 1.4810*/811public int getExtendedState() {812synchronized (getObjectLock()) {813return state;814}815}816817static {818AWTAccessor.setFrameAccessor(819new AWTAccessor.FrameAccessor() {820public void setExtendedState(Frame frame, int state) {821synchronized(frame.getObjectLock()) {822frame.state = state;823}824}825public int getExtendedState(Frame frame) {826synchronized(frame.getObjectLock()) {827return frame.state;828}829}830public Rectangle getMaximizedBounds(Frame frame) {831synchronized(frame.getObjectLock()) {832return frame.maximizedBounds;833}834}835}836);837}838839/**840* Sets the maximized bounds for this frame.841* <p>842* When a frame is in maximized state the system supplies some843* defaults bounds. This method allows some or all of those844* system supplied values to be overridden.845* <p>846* If <code>bounds</code> is <code>null</code>, accept bounds847* supplied by the system. If non-<code>null</code> you can848* override some of the system supplied values while accepting849* others by setting those fields you want to accept from system850* to <code>Integer.MAX_VALUE</code>.851* <p>852* Note, the given maximized bounds are used as a hint for the native853* system, because the underlying platform may not support setting the854* location and/or size of the maximized windows. If that is the case, the855* provided values do not affect the appearance of the frame in the856* maximized state.857*858* @param bounds bounds for the maximized state859* @see #getMaximizedBounds()860* @since 1.4861*/862public void setMaximizedBounds(Rectangle bounds) {863synchronized(getObjectLock()) {864this.maximizedBounds = bounds;865}866FramePeer peer = (FramePeer)this.peer;867if (peer != null) {868peer.setMaximizedBounds(bounds);869}870}871872/**873* Gets maximized bounds for this frame.874* Some fields may contain <code>Integer.MAX_VALUE</code> to indicate875* that system supplied values for this field must be used.876*877* @return maximized bounds for this frame; may be <code>null</code>878* @see #setMaximizedBounds(Rectangle)879* @since 1.4880*/881public Rectangle getMaximizedBounds() {882synchronized(getObjectLock()) {883return maximizedBounds;884}885}886887888/**889* Disables or enables decorations for this frame.890* <p>891* This method can only be called while the frame is not displayable. To892* make this frame decorated, it must be opaque and have the default shape,893* otherwise the {@code IllegalComponentStateException} will be thrown.894* Refer to {@link Window#setShape}, {@link Window#setOpacity} and {@link895* Window#setBackground} for details896*897* @param undecorated {@code true} if no frame decorations are to be898* enabled; {@code false} if frame decorations are to be enabled899*900* @throws IllegalComponentStateException if the frame is displayable901* @throws IllegalComponentStateException if {@code undecorated} is902* {@code false}, and this frame does not have the default shape903* @throws IllegalComponentStateException if {@code undecorated} is904* {@code false}, and this frame opacity is less than {@code 1.0f}905* @throws IllegalComponentStateException if {@code undecorated} is906* {@code false}, and the alpha value of this frame background907* color is less than {@code 1.0f}908*909* @see #isUndecorated910* @see Component#isDisplayable911* @see Window#getShape912* @see Window#getOpacity913* @see Window#getBackground914* @see javax.swing.JFrame#setDefaultLookAndFeelDecorated(boolean)915*916* @since 1.4917*/918public void setUndecorated(boolean undecorated) {919/* Make sure we don't run in the middle of peer creation.*/920synchronized (getTreeLock()) {921if (isDisplayable()) {922throw new IllegalComponentStateException("The frame is displayable.");923}924if (!undecorated) {925if (getOpacity() < 1.0f) {926throw new IllegalComponentStateException("The frame is not opaque");927}928if (getShape() != null) {929throw new IllegalComponentStateException("The frame does not have a default shape");930}931Color bg = getBackground();932if ((bg != null) && (bg.getAlpha() < 255)) {933throw new IllegalComponentStateException("The frame background color is not opaque");934}935}936this.undecorated = undecorated;937}938}939940/**941* Indicates whether this frame is undecorated.942* By default, all frames are initially decorated.943* @return <code>true</code> if frame is undecorated;944* <code>false</code> otherwise.945* @see java.awt.Frame#setUndecorated(boolean)946* @since 1.4947*/948public boolean isUndecorated() {949return undecorated;950}951952/**953* {@inheritDoc}954*/955@Override956public void setOpacity(float opacity) {957synchronized (getTreeLock()) {958if ((opacity < 1.0f) && !isUndecorated()) {959throw new IllegalComponentStateException("The frame is decorated");960}961super.setOpacity(opacity);962}963}964965/**966* {@inheritDoc}967*/968@Override969public void setShape(Shape shape) {970synchronized (getTreeLock()) {971if ((shape != null) && !isUndecorated()) {972throw new IllegalComponentStateException("The frame is decorated");973}974super.setShape(shape);975}976}977978/**979* {@inheritDoc}980*/981@Override982public void setBackground(Color bgColor) {983synchronized (getTreeLock()) {984if ((bgColor != null) && (bgColor.getAlpha() < 255) && !isUndecorated()) {985throw new IllegalComponentStateException("The frame is decorated");986}987super.setBackground(bgColor);988}989}990991/**992* Removes the specified menu bar from this frame.993* @param m the menu component to remove.994* If <code>m</code> is <code>null</code>, then995* no action is taken996*/997public void remove(MenuComponent m) {998if (m == null) {999return;1000}1001synchronized (getTreeLock()) {1002if (m == menuBar) {1003menuBar = null;1004FramePeer peer = (FramePeer)this.peer;1005if (peer != null) {1006mbManagement = true;1007invalidateIfValid();1008peer.setMenuBar(null);1009m.removeNotify();1010}1011m.parent = null;1012} else {1013super.remove(m);1014}1015}1016}10171018/**1019* Makes this Frame undisplayable by removing its connection1020* to its native screen resource. Making a Frame undisplayable1021* will cause any of its children to be made undisplayable.1022* This method is called by the toolkit internally and should1023* not be called directly by programs.1024* @see Component#isDisplayable1025* @see #addNotify1026*/1027public void removeNotify() {1028synchronized (getTreeLock()) {1029FramePeer peer = (FramePeer)this.peer;1030if (peer != null) {1031// get the latest Frame state before disposing1032getState();10331034if (menuBar != null) {1035mbManagement = true;1036peer.setMenuBar(null);1037menuBar.removeNotify();1038}1039}1040super.removeNotify();1041}1042}10431044void postProcessKeyEvent(KeyEvent e) {1045if (menuBar != null && menuBar.handleShortcut(e)) {1046e.consume();1047return;1048}1049super.postProcessKeyEvent(e);1050}10511052/**1053* Returns a string representing the state of this <code>Frame</code>.1054* This method is intended to be used only for debugging purposes, and the1055* content and format of the returned string may vary between1056* implementations. The returned string may be empty but may not be1057* <code>null</code>.1058*1059* @return the parameter string of this frame1060*/1061protected String paramString() {1062String str = super.paramString();1063if (title != null) {1064str += ",title=" + title;1065}1066if (resizable) {1067str += ",resizable";1068}1069int state = getExtendedState();1070if (state == NORMAL) {1071str += ",normal";1072}1073else {1074if ((state & ICONIFIED) != 0) {1075str += ",iconified";1076}1077if ((state & MAXIMIZED_BOTH) == MAXIMIZED_BOTH) {1078str += ",maximized";1079}1080else if ((state & MAXIMIZED_HORIZ) != 0) {1081str += ",maximized_horiz";1082}1083else if ((state & MAXIMIZED_VERT) != 0) {1084str += ",maximized_vert";1085}1086}1087return str;1088}10891090/**1091* @deprecated As of JDK version 1.1,1092* replaced by <code>Component.setCursor(Cursor)</code>.1093*/1094@Deprecated1095public void setCursor(int cursorType) {1096if (cursorType < DEFAULT_CURSOR || cursorType > MOVE_CURSOR) {1097throw new IllegalArgumentException("illegal cursor type");1098}1099setCursor(Cursor.getPredefinedCursor(cursorType));1100}11011102/**1103* @deprecated As of JDK version 1.1,1104* replaced by <code>Component.getCursor()</code>.1105*/1106@Deprecated1107public int getCursorType() {1108return (getCursor().getType());1109}11101111/**1112* Returns an array of all {@code Frame}s created by this application.1113* If called from an applet, the array includes only the {@code Frame}s1114* accessible by that applet.1115* <p>1116* <b>Warning:</b> this method may return system created frames, such1117* as a shared, hidden frame which is used by Swing. Applications1118* should not assume the existence of these frames, nor should an1119* application assume anything about these frames such as component1120* positions, <code>LayoutManager</code>s or serialization.1121* <p>1122* <b>Note</b>: To obtain a list of all ownerless windows, including1123* ownerless {@code Dialog}s (introduced in release 1.6), use {@link1124* Window#getOwnerlessWindows Window.getOwnerlessWindows}.1125*1126* @see Window#getWindows()1127* @see Window#getOwnerlessWindows1128*1129* @since 1.21130*/1131public static Frame[] getFrames() {1132Window[] allWindows = Window.getWindows();11331134int frameCount = 0;1135for (Window w : allWindows) {1136if (w instanceof Frame) {1137frameCount++;1138}1139}11401141Frame[] frames = new Frame[frameCount];1142int c = 0;1143for (Window w : allWindows) {1144if (w instanceof Frame) {1145frames[c++] = (Frame)w;1146}1147}11481149return frames;1150}11511152/* Serialization support. If there's a MenuBar we restore1153* its (transient) parent field here. Likewise for top level1154* windows that are "owned" by this frame.1155*/11561157/**1158* <code>Frame</code>'s Serialized Data Version.1159*1160* @serial1161*/1162private int frameSerializedDataVersion = 1;11631164/**1165* Writes default serializable fields to stream. Writes1166* an optional serializable icon <code>Image</code>, which is1167* available as of 1.4.1168*1169* @param s the <code>ObjectOutputStream</code> to write1170* @serialData an optional icon <code>Image</code>1171* @see java.awt.Image1172* @see #getIconImage1173* @see #setIconImage(Image)1174* @see #readObject(ObjectInputStream)1175*/1176private void writeObject(ObjectOutputStream s)1177throws IOException1178{1179s.defaultWriteObject();1180if (icons != null && icons.size() > 0) {1181Image icon1 = icons.get(0);1182if (icon1 instanceof Serializable) {1183s.writeObject(icon1);1184return;1185}1186}1187s.writeObject(null);1188}11891190/**1191* Reads the <code>ObjectInputStream</code>. Tries1192* to read an icon <code>Image</code>, which is optional1193* data available as of 1.4. If an icon <code>Image</code>1194* is not available, but anything other than an EOF1195* is detected, an <code>OptionalDataException</code>1196* will be thrown.1197* Unrecognized keys or values will be ignored.1198*1199* @param s the <code>ObjectInputStream</code> to read1200* @exception java.io.OptionalDataException if an icon <code>Image</code>1201* is not available, but anything other than an EOF1202* is detected1203* @exception HeadlessException if1204* <code>GraphicsEnvironment.isHeadless</code> returns1205* <code>true</code>1206* @see java.awt.GraphicsEnvironment#isHeadless()1207* @see java.awt.Image1208* @see #getIconImage1209* @see #setIconImage(Image)1210* @see #writeObject(ObjectOutputStream)1211*/1212private void readObject(ObjectInputStream s)1213throws ClassNotFoundException, IOException, HeadlessException1214{1215// HeadlessException is thrown by Window's readObject1216s.defaultReadObject();1217try {1218Image icon = (Image) s.readObject();1219if (icons == null) {1220icons = new ArrayList<Image>();1221icons.add(icon);1222}1223} catch (java.io.OptionalDataException e) {1224// pre-1.4 instances will not have this optional data.1225// 1.6 and later instances serialize icons in the Window class1226// e.eof will be true to indicate that there is no more1227// data available for this object.12281229// If e.eof is not true, throw the exception as it1230// might have been caused by unrelated reasons.1231if (!e.eof) {1232throw (e);1233}1234}12351236if (menuBar != null)1237menuBar.parent = this;12381239// Ensure 1.1 serialized Frames can read & hook-up1240// owned windows properly1241//1242if (ownedWindows != null) {1243for (int i = 0; i < ownedWindows.size(); i++) {1244connectOwnedWindow(ownedWindows.elementAt(i));1245}1246ownedWindows = null;1247}1248}12491250/**1251* Initialize JNI field and method IDs1252*/1253private static native void initIDs();12541255/*1256* --- Accessibility Support ---1257*1258*/12591260/**1261* Gets the AccessibleContext associated with this Frame.1262* For frames, the AccessibleContext takes the form of an1263* AccessibleAWTFrame.1264* A new AccessibleAWTFrame instance is created if necessary.1265*1266* @return an AccessibleAWTFrame that serves as the1267* AccessibleContext of this Frame1268* @since 1.31269*/1270public AccessibleContext getAccessibleContext() {1271if (accessibleContext == null) {1272accessibleContext = new AccessibleAWTFrame();1273}1274return accessibleContext;1275}12761277/**1278* This class implements accessibility support for the1279* <code>Frame</code> class. It provides an implementation of the1280* Java Accessibility API appropriate to frame user-interface elements.1281* @since 1.31282*/1283protected class AccessibleAWTFrame extends AccessibleAWTWindow1284{1285/*1286* JDK 1.3 serialVersionUID1287*/1288private static final long serialVersionUID = -6172960752956030250L;12891290/**1291* Get the role of this object.1292*1293* @return an instance of AccessibleRole describing the role of the1294* object1295* @see AccessibleRole1296*/1297public AccessibleRole getAccessibleRole() {1298return AccessibleRole.FRAME;1299}13001301/**1302* Get the state of this object.1303*1304* @return an instance of AccessibleStateSet containing the current1305* state set of the object1306* @see AccessibleState1307*/1308public AccessibleStateSet getAccessibleStateSet() {1309AccessibleStateSet states = super.getAccessibleStateSet();1310if (getFocusOwner() != null) {1311states.add(AccessibleState.ACTIVE);1312}1313if (isResizable()) {1314states.add(AccessibleState.RESIZABLE);1315}1316return states;1317}131813191320} // inner class AccessibleAWTFrame13211322}132313241325