📄 container.java
字号:
/* Container.java -- parent container class in AWT Copyright (C) 1999, 2000, 2002 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., 59 Temple Place, Suite 330, Boston, MA02111-1307 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.AWTEventListener;import java.awt.event.ContainerEvent;import java.awt.event.ContainerListener;import java.awt.event.MouseEvent;import java.awt.peer.ComponentPeer;import java.awt.peer.ContainerPeer;import java.awt.peer.LightweightPeer;import java.beans.PropertyChangeListener;import java.io.PrintStream;import java.io.PrintWriter;import java.io.Serializable;import java.util.EventListener;import java.util.Set;import javax.accessibility.Accessible;/** * A generic window toolkit object that acts as a container for other objects. * Components are tracked in a list, and new elements are at the end of the * list or bottom of the stacking order. * * @author original author unknown * @author Eric Blake <ebb9@email.byu.edu> * * @since 1.0 * * @status still missing 1.4 support */public class Container extends Component{ /** * Compatible with JDK 1.0+. */ private static final long serialVersionUID = 4613797578919906343L; /* Serialized fields from the serialization spec. */ int ncomponents; Component[] component; LayoutManager layoutMgr; LightweightDispatcher dispatcher; Dimension maxSize; /** * @since 1.4 */ boolean focusCycleRoot; int containerSerializedDataVersion; /* Anything else is non-serializable, and should be declared "transient". */ transient ContainerListener containerListener; /** * Default constructor for subclasses. */ public Container() { } /** * Returns the number of components in this container. * * @return The number of components in this container. */ public int getComponentCount() { return ncomponents; } /** * Returns the number of components in this container. * * @return The number of components in this container. * * @deprecated use {@link #getComponentCount()} instead */ public int countComponents() { return getComponentCount(); } /** * Returns the component at the specified index. * * @param index The index of the component to retrieve. * * @return The requested component. * * @throws ArrayIndexOutOfBoundsException If the specified index is invalid */ public Component getComponent(int n) { synchronized (getTreeLock ()) { if (n < 0 || n >= ncomponents) throw new ArrayIndexOutOfBoundsException("no such component"); return component[n]; } } /** * Returns an array of the components in this container. * * @return The components in this container. */ public Component[] getComponents() { synchronized (getTreeLock ()) { Component[] result = new Component[ncomponents]; if (ncomponents > 0) System.arraycopy(component, 0, result, 0, ncomponents); return result; } } /** * Returns the insets for this container, which is the space used for * borders, the margin, etc. * * @return The insets for this container. */ public Insets getInsets() { if (peer == null) return new Insets(0, 0, 0, 0); return ((ContainerPeer) peer).getInsets(); } /** * Returns the insets for this container, which is the space used for * borders, the margin, etc. * * @return The insets for this container. * @deprecated use {@link #getInsets()} instead */ public Insets insets() { return getInsets(); } /** * Adds the specified component to this container at the end of the * component list. * * @param component The component to add to the container. * * @return The same component that was added. */ public Component add(Component comp) { addImpl(comp, null, -1); return comp; } /** * Adds the specified component to the container at the end of the * component list. This method should not be used. Instead, use * <code>add(Component, Object</code>. * * @param name The name of the component to be added. * @param component The component to be added. * * @return The same component that was added. */ public Component add(String name, Component comp) { addImpl(comp, name, -1); return comp; } /** * Adds the specified component to this container at the specified index * in the component list. * * @param component The component to be added. * @param index The index in the component list to insert this child * at, or -1 to add at the end of the list. * * @return The same component that was added. * * @throws ArrayIndexOutOfBounds If the specified index is invalid. */ public Component add(Component comp, int index) { addImpl(comp, null, index); return comp; } /** * Adds the specified component to this container at the end of the * component list. The layout manager will use the specified constraints * when laying out this component. * * @param component The component to be added to this container. * @param constraints The layout constraints for this component. */ public void add(Component comp, Object constraints) { addImpl(comp, constraints, -1); } /** * Adds the specified component to this container at the specified index * in the component list. The layout manager will use the specified * constraints when layout out this component. * * @param component The component to be added. * @param constraints The layout constraints for this component. * @param index The index in the component list to insert this child * at, or -1 to add at the end of the list. * * @throws ArrayIndexOutOfBounds If the specified index is invalid. */ public void add(Component comp, Object constraints, int index) { addImpl(comp, constraints, index); } /** * This method is called by all the <code>add()</code> methods to perform * the actual adding of the component. Subclasses who wish to perform * their own processing when a component is added should override this * method. Any subclass doing this must call the superclass version of * this method in order to ensure proper functioning of the container. * * @param component The component to be added. * @param constraints The layout constraints for this component, or * <code>null</code> if there are no constraints. * @param index The index in the component list to insert this child * at, or -1 to add at the end of the list. * * @throws ArrayIndexOutOfBounds If the specified index is invalid. */ protected void addImpl(Component comp, Object constraints, int index) { synchronized (getTreeLock ()) { if (index > ncomponents || (index < 0 && index != -1) || comp instanceof Window || (comp instanceof Container && ((Container) comp).isAncestorOf(this))) throw new IllegalArgumentException(); // Reparent component, and make sure component is instantiated if // we are. if (comp.parent != null) comp.parent.remove(comp); comp.parent = this; if (peer != null) { comp.addNotify(); if (comp.isLightweight()) enableEvents(comp.eventMask); } invalidate(); if (component == null) component = new Component[4]; // FIXME, better initial size? // This isn't the most efficient implementation. We could do less // copying when growing the array. It probably doesn't matter. if (ncomponents >= component.length) { int nl = component.length * 2; Component[] c = new Component[nl]; System.arraycopy(component, 0, c, 0, ncomponents); component = c; } if (index == -1) component[ncomponents++] = comp; else { System.arraycopy(component, index, component, index + 1, ncomponents - index); component[index] = comp; ++ncomponents; } // Notify the layout manager. if (layoutMgr != null) { if (layoutMgr instanceof LayoutManager2) { LayoutManager2 lm2 = (LayoutManager2) layoutMgr; lm2.addLayoutComponent(comp, constraints); } else if (constraints instanceof String) layoutMgr.addLayoutComponent((String) constraints, comp); else layoutMgr.addLayoutComponent(null, comp); } // Post event to notify of adding the container. ContainerEvent ce = new ContainerEvent(this, ContainerEvent.COMPONENT_ADDED, comp); getToolkit().getSystemEventQueue().postEvent(ce); } } /** * Removes the component at the specified index from this container. * * @param index The index of the component to remove. */ public void remove(int index) { synchronized (getTreeLock ()) { Component r = component[index]; r.removeNotify(); System.arraycopy(component, index + 1, component, index, ncomponents - index - 1); component[--ncomponents] = null; invalidate(); if (layoutMgr != null) layoutMgr.removeLayoutComponent(r); // Post event to notify of adding the container. ContainerEvent ce = new ContainerEvent(this, ContainerEvent.COMPONENT_REMOVED, r); getToolkit().getSystemEventQueue().postEvent(ce); } } /** * Removes the specified component from this container. * * @return component The component to remove from this container. */ public void remove(Component comp) { synchronized (getTreeLock ()) { for (int i = 0; i < ncomponents; ++i) { if (component[i] == comp) { remove(i); break; } } } } /** * Removes all components from this container. */ public void removeAll() { synchronized (getTreeLock ()) { while (ncomponents > 0) remove(0); } } /** * Returns the current layout manager for this container. * * @return The layout manager for this container. */ public LayoutManager getLayout() { return layoutMgr; } /** * Sets the layout manager for this container to the specified layout * manager. * * @param mgr The new layout manager for this container. */ public void setLayout(LayoutManager mgr) { layoutMgr = mgr; invalidate(); } /** * Layout the components in this container. */ public void doLayout() { if (layoutMgr != null) layoutMgr.layoutContainer(this); } /** * Layout the components in this container. * * @deprecated use {@link #doLayout()} instead */ public void layout() { doLayout(); } /** * Invalidates this container to indicate that it (and all parent * containers) need to be laid out. */ public void invalidate() { super.invalidate(); } /** * Re-lays out the components in this container. */ public void validate() { synchronized (getTreeLock ()) { if (! isValid()) { validateTree(); } } } /** * Recursively validates the container tree, recomputing any invalid * layouts. */ protected void validateTree() { if (valid)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -