📄 window.java
字号:
/* Window.java -- Copyright (C) 1999, 2000, 2002, 2003, 2004 Free Software FoundationThis file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.awt;import java.awt.event.ComponentEvent;import java.awt.event.FocusEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.event.WindowFocusListener;import java.awt.event.WindowListener;import java.awt.event.WindowStateListener;import java.awt.image.BufferStrategy;import java.awt.peer.WindowPeer;import java.lang.ref.Reference;import java.lang.ref.WeakReference;import java.util.EventListener;import java.util.Iterator;import java.util.Locale;import java.util.ResourceBundle;import java.util.Vector;import javax.accessibility.Accessible;import javax.accessibility.AccessibleContext;import javax.accessibility.AccessibleRole;import javax.accessibility.AccessibleState;import javax.accessibility.AccessibleStateSet;/** * This class represents a top-level window with no decorations. * * @author Aaron M. Renn (arenn@urbanophile.com) * @author Warren Levy (warrenl@cygnus.com) */public class Window extends Container implements Accessible{ private static final long serialVersionUID = 4497834738069338734L; // Serialized fields, from Sun's serialization spec. private String warningString = null; private int windowSerializedDataVersion = 0; // FIXME /** @since 1.2 */ // private FocusManager focusMgr; // FIXME: what is this? /** @since 1.2 */ private int state = 0; /** @since 1.4 */ private boolean focusableWindowState = true; // A list of other top-level windows owned by this window. private transient Vector ownedWindows = new Vector(); private transient WindowListener windowListener; private transient WindowFocusListener windowFocusListener; private transient WindowStateListener windowStateListener; private transient GraphicsConfiguration graphicsConfiguration; private transient boolean shown; // This is package-private to avoid an accessor method. transient Component windowFocusOwner; /* * The number used to generate the name returned by getName. */ private static transient long next_window_number; protected class AccessibleAWTWindow extends AccessibleAWTContainer { private static final long serialVersionUID = 4215068635060671780L; public AccessibleRole getAccessibleRole() { return AccessibleRole.WINDOW; } public AccessibleStateSet getAccessibleStateSet() { AccessibleStateSet states = super.getAccessibleStateSet(); if (isActive()) states.add(AccessibleState.ACTIVE); return states; } } /** * This (package access) constructor is used by subclasses that want * to build windows that do not have parents. Eg. toplevel * application frames. Subclasses cannot call super(null), since * null is an illegal argument. */ Window() { visible = false; // Windows are the only Containers that default to being focus // cycle roots. focusCycleRoot = true; setLayout(new BorderLayout()); addWindowFocusListener (new WindowAdapter () { public void windowGainedFocus (WindowEvent event) { if (windowFocusOwner != null) { // FIXME: move this section and the other similar // sections in Component into a separate method. EventQueue eq = Toolkit.getDefaultToolkit ().getSystemEventQueue (); synchronized (eq) { KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager (); Component currentFocusOwner = manager.getGlobalPermanentFocusOwner (); if (currentFocusOwner != null) { eq.postEvent (new FocusEvent (currentFocusOwner, FocusEvent.FOCUS_LOST, false, windowFocusOwner)); eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED, false, currentFocusOwner)); } else eq.postEvent (new FocusEvent (windowFocusOwner, FocusEvent.FOCUS_GAINED, false)); } } } }); GraphicsEnvironment g = GraphicsEnvironment.getLocalGraphicsEnvironment(); graphicsConfiguration = g.getDefaultScreenDevice().getDefaultConfiguration(); } Window(GraphicsConfiguration gc) { this(); graphicsConfiguration = gc; } /** * Initializes a new instance of <code>Window</code> with the specified * parent. The window will initially be invisible. * * @param owner The owning <code>Frame</code> of this window. * * @exception IllegalArgumentException If the owner's GraphicsConfiguration * is not from a screen device, or if owner is null; this exception is always * thrown when GraphicsEnvironment.isHeadless returns true. */ public Window(Frame owner) { this (owner, owner.getGraphicsConfiguration ()); } /** * Initializes a new instance of <code>Window</code> with the specified * parent. The window will initially be invisible. * * @exception IllegalArgumentException If the owner's GraphicsConfiguration * is not from a screen device, or if owner is null; this exception is always * thrown when GraphicsEnvironment.isHeadless returns true. * * @since 1.2 */ public Window(Window owner) { this (owner, owner.getGraphicsConfiguration ()); } /** * Initializes a new instance of <code>Window</code> with the specified * parent. The window will initially be invisible. * * @exception IllegalArgumentException If owner is null or if gc is not from a * screen device; this exception is always thrown when * GraphicsEnvironment.isHeadless returns true. * * @since 1.3 */ public Window(Window owner, GraphicsConfiguration gc) { this (); synchronized (getTreeLock()) { if (owner == null) throw new IllegalArgumentException ("owner must not be null"); parent = owner; owner.ownedWindows.add(new WeakReference(this)); } // FIXME: make this text visible in the window. SecurityManager s = System.getSecurityManager(); if (s != null && ! s.checkTopLevelWindow(this)) warningString = System.getProperty("awt.appletWarning"); if (gc != null && gc.getDevice().getType() != GraphicsDevice.TYPE_RASTER_SCREEN) throw new IllegalArgumentException ("gc must be from a screen device"); if (gc == null) graphicsConfiguration = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration(); else graphicsConfiguration = gc; } GraphicsConfiguration getGraphicsConfigurationImpl() { if (graphicsConfiguration != null) return graphicsConfiguration; return super.getGraphicsConfigurationImpl(); } /** * Creates the native peer for this window. */ public void addNotify() { if (peer == null) peer = getToolkit().createWindow(this); super.addNotify(); } /** * Relays out this window's child components at their preferred size. * * @specnote pack() doesn't appear to be called internally by show(), so * we duplicate some of the functionality. */ public void pack() { if (parent != null && !parent.isDisplayable()) parent.addNotify(); if (peer == null) addNotify(); setSize(getPreferredSize()); validate(); } /** * Shows on-screen this window and any of its owned windows for whom * isVisible returns true. */ public void show() { synchronized (getTreeLock()) { if (parent != null && !parent.isDisplayable()) parent.addNotify(); if (peer == null) addNotify(); // Show visible owned windows. Iterator e = ownedWindows.iterator(); while(e.hasNext()) { Window w = (Window)(((Reference) e.next()).get()); if (w != null) { if (w.isVisible()) w.getPeer().setVisible(true); } else // Remove null weak reference from ownedWindows. // Unfortunately this can't be done in the Window's // finalize method because there is no way to guarantee // synchronous access to ownedWindows there. e.remove(); } validate(); super.show(); toFront(); KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager (); manager.setGlobalFocusedWindow (this); if (!shown) { FocusTraversalPolicy policy = getFocusTraversalPolicy (); Component initialFocusOwner = null; if (policy != null) initialFocusOwner = policy.getInitialComponent (this); if (initialFocusOwner != null) initialFocusOwner.requestFocusInWindow (); shown = true; } } } public void hide() { // Hide visible owned windows. synchronized (getTreeLock ()) { Iterator e = ownedWindows.iterator(); while(e.hasNext()) { Window w = (Window)(((Reference) e.next()).get()); if (w != null) { if (w.isVisible() && w.getPeer() != null) w.getPeer().setVisible(false); } else e.remove(); } } super.hide(); } /** * Destroys any resources associated with this window. This includes * all components in the window and all owned top-level windows. */ public void dispose() { hide(); synchronized (getTreeLock ()) { Iterator e = ownedWindows.iterator(); while(e.hasNext()) { Window w = (Window)(((Reference) e.next()).get()); if (w != null) w.dispose(); else // Remove null weak reference from ownedWindows. e.remove(); } for (int i = 0; i < ncomponents; ++i) component[i].removeNotify(); this.removeNotify(); // Post a WINDOW_CLOSED event. WindowEvent we = new WindowEvent(this, WindowEvent.WINDOW_CLOSED); getToolkit().getSystemEventQueue().postEvent(we); } } /** * Sends this window to the back so that all other windows display in * front of it. */ public void toBack() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -