container.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,606 行 · 第 1/4 页
JAVA
1,606 行
/* * @(#)Container.java 1.18 06/10/10 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */package java.awt;import java.io.PrintStream;import java.io.PrintWriter;import java.awt.event.ComponentEvent;import java.awt.event.ContainerEvent;import java.awt.event.FocusEvent;import java.awt.event.InputEvent;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.awt.event.ContainerListener;import java.awt.event.AWTEventListener;import java.util.EventListener;import java.io.ObjectOutputStream;import java.io.ObjectInputStream;import java.io.IOException;import java.awt.event.WindowAdapter;import java.awt.event.WindowListener;import java.awt.event.WindowEvent;import java.util.Set;import java.util.Stack;import java.util.Vector;import sun.awt.ConstrainableGraphics;/** * A generic Abstract Window Toolkit(AWT) container object is a component * that can contain other AWT components. * <p> * Components added to a container are tracked in a list. The order * of the list will define the components' front-to-back stacking order * within the container. If no index is specified when adding a * component to a container, it will be added to the end of the list * (and hence to the bottom of the stacking order). * @version 1.181, 04/06/00 * @author Arthur van Hoff * @author Sami Shaio * @see java.awt.Container#add(java.awt.Component, int) * @see java.awt.Container#getComponent(int) * @see java.awt.LayoutManager * @since JDK1.0 */public class Container extends Component { /** * The number of components in this container. * This value can be null. * @serial * @see getComponent() * @see getComponents() * @see getComponentCount() */ int ncomponents; /** * The components in this container. * @serial * @see add() * @see getComponents() */ Component component[] = new Component[4]; /** * Layout manager for this container. * @serial * @see doLayout() * @see setLayout() * @see getLayout() */ LayoutManager layoutMgr; // ### Serialization issue: LightweightDispatcher is package-private /** * Event router for lightweight components. If this container * is native, this dispatcher takes care of forwarding and * retargeting the events to lightweight components contained * (if any). * @serial */ //private LightweightDispatcher dispatcher; transient ContainerListener containerListener; /* * Internal, cached size information. * @serial * @see getMaximumSize() * @see getPreferredSize() */ private Dimension maxSize; /* * JDK 1.1 serialVersionUID */ private static final long serialVersionUID = 4613797578919906343L; /** * Constructs a new Container. Containers can be extended directly, * but are lightweight in this case and must be contained by a parent * somewhere higher up in the component tree that is native. * (such as Frame for example). */ public Container() {} /** * Gets the number of components in this panel. * @return the number of components in this panel. * @see java.awt.Container#getComponent * @since JDK1.1 */ public int getComponentCount() { return ncomponents; } /** * Gets the nth component in this container. * @param n the index of the component to get. * @return the n<sup>th</sup> component in this container. * @exception ArrayIndexOutOfBoundsException * if the n<sup>th</sup> value does not exist. */ public Component getComponent(int n) { synchronized (getTreeLock()) { if ((n < 0) || (n >= ncomponents)) { throw new ArrayIndexOutOfBoundsException("No such child: " + n); } return component[n]; } } /** * Gets all the components in this container. * @return an array of all the components in this container. */ public Component[] getComponents() { synchronized (getTreeLock()) { Component list[] = new Component[ncomponents]; System.arraycopy(component, 0, list, 0, ncomponents); return list; } } /** * Determines the insets of this container, which indicate the size * of the container's border. * <p> * A <code>Frame</code> object, for example, has a top inset that * corresponds to the height of the frame's title bar. * @return the insets of this container. * @see java.awt.Insets * @see java.awt.LayoutManager * @since JDK1.1 */ public Insets getInsets() { return new Insets(0, 0, 0, 0); } /** * Adds the specified component to the end of this container. * @param comp the component to be added. * @return the component argument. */ public Component add(Component comp) { addImpl(comp, null, -1); return comp; } /** * Adds the specified component to this container. * It is strongly advised to use the 1.1 method, add(Component, Object), * in place of this method. */ public Component add(String name, Component comp) { addImpl(comp, name, -1); return comp; } /** * Adds the specified component to this container at the given * position. * @param comp the component to be added. * @param index the position at which to insert the component, * or <code>-1</code> to insert the component at the end. * @return the component <code>comp</code> * @see #remove */ public Component add(Component comp, int index) { addImpl(comp, null, index); return comp; } /** * Adds the specified component to the end of this container. * Also notifies the layout manager to add the component to * this container's layout using the specified constraints object. * @param comp the component to be added * @param constraints an object expressing * layout contraints for this component * @see java.awt.LayoutManager * @since JDK1.1 */ public void add(Component comp, Object constraints) { addImpl(comp, constraints, -1); } /** * Adds the specified component to this container with the specified * constraints at the specified index. Also notifies the layout * manager to add the component to the this container's layout using * the specified constraints object. * @param comp the component to be added * @param constraints an object expressing layout contraints for this * @param index the position in the container's list at which to insert * the component. -1 means insert at the end. * component * @see #remove * @see LayoutManager */ public void add(Component comp, Object constraints, int index) { addImpl(comp, constraints, index); } /** * Adds the specified component to this container at the specified * index. This method also notifies the layout manager to add * the component to this container's layout using the specified * constraints object. * <p> * This is the method to override if a program needs to track * every add request to a container. An overriding method should * usually include a call to the superclass's version of the method: * <p> * <blockquote> * <code>super.addImpl(comp, constraints, index)</code> * </blockquote> * <p> * @param comp the component to be added. * @param constraints an object expressing layout contraints * for this component. * @param index the position in the container's list at which to * insert the component, where <code>-1</code> * means insert at the end. * @see java.awt.Container#add(java.awt.Component) * @see java.awt.Container#add(java.awt.Component, int) * @see java.awt.Container#add(java.awt.Component, java.lang.Object) * @see java.awt.LayoutManager * @since JDK1.1 */ protected void addImpl(Component comp, Object constraints, int index) { synchronized (getTreeLock()) { /* Check for correct arguments: index in bounds, * comp cannot be one of this container's parents, * and comp cannot be a window. * comp and container must be on the same GraphicsDevice. * if comp is container, all sub-components must be on * same GraphicsDevice. */ //GraphicsConfiguration thisGC = this.getGraphicsConfiguration(); if (index > ncomponents || (index < 0 && index != -1)) { throw new IllegalArgumentException( "illegal component position"); } if (comp instanceof Container) { for (Container cn = this; cn != null; cn = cn.parent) { if (cn == comp) { throw new IllegalArgumentException( "adding container's parent to itself"); } } if (comp instanceof Window) { throw new IllegalArgumentException( "adding a window to a container"); } } /* Reparent the component and tidy up the tree's state. */ if (comp.parent != null) { comp.parent.remove(comp); // 6258389 if (index > ncomponents) { throw new IllegalArgumentException("illegal component position"); } // 6258389 } /* Add component to list; allocate new array if necessary. */ if (ncomponents == component.length) { Component newcomponents[] = new Component[ncomponents * 2]; System.arraycopy(component, 0, newcomponents, 0, ncomponents); component = newcomponents; } if (index == -1 || index == ncomponents) { component[ncomponents++] = comp; } else { System.arraycopy(component, index, component, index + 1, ncomponents - index); component[index] = comp; ncomponents++; } comp.parent = this; if (valid) { invalidate(); } if (displayable) { comp.addNotify(); } /* Notify the layout manager of the added component. */ if (layoutMgr != null) { if (layoutMgr instanceof LayoutManager2) { ((LayoutManager2) layoutMgr).addLayoutComponent(comp, constraints); } else if (constraints instanceof String) { layoutMgr.addLayoutComponent((String) constraints, comp); } } if (containerListener != null || (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0) { ContainerEvent e = new ContainerEvent(this, ContainerEvent.COMPONENT_ADDED, comp); dispatchEvent(e); } } } /** * Removes the component, specified by <code>index</code>, * from this container. * @param index the index of the component to be removed. * @see #add * @since JDK1.1 */ public void remove(int index) { synchronized (getTreeLock()) { /* 4629242 - Check if Container contains any components or if * index references a null array element. */ if (ncomponents == 0 || component[index] == null) { throw new IllegalArgumentException("Illegal Argument : " + index + "."); } Component comp = component[index]; if (displayable) { comp.removeNotify(); } if (layoutMgr != null) { layoutMgr.removeLayoutComponent(comp); } comp.parent = null; System.arraycopy(component, index + 1, component, index, ncomponents - index - 1); component[--ncomponents] = null; if (valid) { invalidate(); } if (containerListener != null || (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0) { ContainerEvent e = new ContainerEvent(this, ContainerEvent.COMPONENT_REMOVED, comp); dispatchEvent(e); } } } /** * Removes the specified component from this container. * @param comp the component to be removed * @see #add */ public void remove(Component comp) { synchronized (getTreeLock()) { if (comp.parent == this) { /* Search backwards, expect that more recent additions * are more likely to be removed. */ Component component[] = this.component; for (int i = ncomponents; --i >= 0;) { if (component[i] == comp) { remove(i); } } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?