container.java

来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 2,024 行 · 第 1/4 页

JAVA
2,024
字号
  }  /**   * 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() && peer != null)          {            validateTree();          }      }  }  /**   * Recursively invalidates the container tree.   */  void invalidateTree()  {    for (int i = 0; i < ncomponents; i++)      {        Component comp = component[i];        comp.invalidate();        if (comp instanceof Container)          ((Container) comp).invalidateTree();      }  }  /**   * Recursively validates the container tree, recomputing any invalid   * layouts.   */  protected void validateTree()  {    if (valid)      return;    ContainerPeer cPeer = null;    if (peer != null && ! (peer instanceof LightweightPeer))      {        cPeer = (ContainerPeer) peer;        cPeer.beginValidate();      }    for (int i = 0; i < ncomponents; ++i)      {        Component comp = component[i];        if (comp.getPeer () == null)          comp.addNotify();      }    doLayout ();    for (int i = 0; i < ncomponents; ++i)      {        Component comp = component[i];        if (! comp.isValid())          {            if (comp instanceof Container)              {                ((Container) comp).validateTree();              }            else              {                component[i].validate();              }          }      }    /* children will call invalidate() when they are layed out. It       is therefore important that valid is not set to true       until after the children have been layed out. */    valid = true;    if (cPeer != null)      cPeer.endValidate();  }  public void setFont(Font f)  {    super.setFont(f);    // FIXME: Although it might make more sense to invalidate only    // those children whose font == null, Sun invalidates all children.    // So we'll do the same.    invalidateTree();  }  /**   * Returns the preferred size of this container.   *   * @return The preferred size of this container.   */  public Dimension getPreferredSize()  {    return preferredSize ();  }  /**   * Returns the preferred size of this container.   *   * @return The preferred size of this container.   *   * @deprecated use {@link #getPreferredSize()} instead   */  public Dimension preferredSize()  {    if (layoutMgr != null)      return layoutMgr.preferredLayoutSize (this);    else      return super.preferredSize ();  }  /**   * Returns the minimum size of this container.   *   * @return The minimum size of this container.   */  public Dimension getMinimumSize()  {    return minimumSize ();  }  /**   * Returns the minimum size of this container.   *   * @return The minimum size of this container.   *   * @deprecated use {@link #getMinimumSize()} instead   */  public Dimension minimumSize()  {    if (layoutMgr != null)      return layoutMgr.minimumLayoutSize (this);    else      return super.minimumSize ();  }  /**   * Returns the maximum size of this container.   *   * @return The maximum size of this container.   */  public Dimension getMaximumSize()  {    if (layoutMgr != null && layoutMgr instanceof LayoutManager2)      {        LayoutManager2 lm2 = (LayoutManager2) layoutMgr;        return lm2.maximumLayoutSize(this);      }    else      return super.getMaximumSize();  }  /**   * Returns the preferred alignment along the X axis.  This is a value   * between 0 and 1 where 0 represents alignment flush left and   * 1 means alignment flush right, and 0.5 means centered.   *   * @return The preferred alignment along the X axis.   */  public float getAlignmentX()  {    if (layoutMgr instanceof LayoutManager2)      {        LayoutManager2 lm2 = (LayoutManager2) layoutMgr;        return lm2.getLayoutAlignmentX(this);      }    else      return super.getAlignmentX();  }  /**   * Returns the preferred alignment along the Y axis.  This is a value   * between 0 and 1 where 0 represents alignment flush top and   * 1 means alignment flush bottom, and 0.5 means centered.   *   * @return The preferred alignment along the Y axis.   */  public float getAlignmentY()  {    if (layoutMgr instanceof LayoutManager2)      {        LayoutManager2 lm2 = (LayoutManager2) layoutMgr;        return lm2.getLayoutAlignmentY(this);      }    else      return super.getAlignmentY();  }  /**   * Paints this container.  The implementation of this method in this   * class forwards to any lightweight components in this container.  If   * this method is subclassed, this method should still be invoked as   * a superclass method so that lightweight components are properly   * drawn.   *   * @param g The graphics context for this paint job.   */  public void paint(Graphics g)  {    if (!isShowing())      return;    // Paint self first.    super.paint(g);    // Visit heavyweights as well, in case they were    // erased when we cleared the background for this container.    visitChildren(g, GfxPaintVisitor.INSTANCE, false);  }  /**   * Updates this container.  The implementation of this method in this   * class forwards to any lightweight components in this container.  If   * this method is subclassed, this method should still be invoked as   * a superclass method so that lightweight components are properly   * drawn.   *   * @param g The graphics context for this update.   */  public void update(Graphics g)  {    super.update(g);  }  /**   * Prints this container.  The implementation of this method in this   * class forwards to any lightweight components in this container.  If   * this method is subclassed, this method should still be invoked as   * a superclass method so that lightweight components are properly   * drawn.   *   * @param g The graphics context for this print job.   */  public void print(Graphics g)  {    super.print(g);    visitChildren(g, GfxPrintVisitor.INSTANCE, true);  }  /**   * Paints all of the components in this container.   *   * @param g The graphics context for this paint job.   */  public void paintComponents(Graphics g)  {    super.paint(g);    visitChildren(g, GfxPaintAllVisitor.INSTANCE, true);  }  /**   * Prints all of the components in this container.   *   * @param g The graphics context for this print job.   */  public void printComponents(Graphics g)  {    super.paint(g);    visitChildren(g, GfxPrintAllVisitor.INSTANCE, true);  }  /**   * Adds the specified container listener to this object's list of   * container listeners.   *   * @param listener The listener to add.   */  public synchronized void addContainerListener(ContainerListener listener)  {    containerListener = AWTEventMulticaster.add(containerListener, listener);  }  /**   * Removes the specified container listener from this object's list of   * container listeners.   *   * @param listener The listener to remove.   */  public synchronized void removeContainerListener(ContainerListener listener)  {    containerListener = AWTEventMulticaster.remove(containerListener, listener);  }  /**   * @since 1.4   */  public synchronized ContainerListener[] getContainerListeners()  {    return (ContainerListener[])      AWTEventMulticaster.getListeners(containerListener,                                       ContainerListener.class);  }  /**   * Returns an array of all the objects currently registered as FooListeners   * upon this Container. FooListeners are registered using the addFooListener   * method.   *   * @exception ClassCastException If listenerType doesn't specify a class or   * interface that implements @see java.util.EventListener.   *   * @since 1.3   */  public EventListener[] getListeners(Class listenerType)  {    if (listenerType == ContainerListener.class)      return getContainerListeners();    return super.getListeners(listenerType);  }  /**   * Processes the specified event.  This method calls   * <code>processContainerEvent()</code> if this method is a   * <code>ContainerEvent</code>, otherwise it calls the superclass   * method.   *   * @param e The event to be processed.   */  protected void processEvent(AWTEvent e)  {    if (e instanceof ContainerEvent)      processContainerEvent((ContainerEvent) e);    else      super.processEvent(e);  }  /**   * Called when a container event occurs if container events are enabled.   * This method calls any registered listeners.   *   * @param e The event that occurred.   */  protected void processContainerEvent(ContainerEvent e)  {    if (containerListener == null)      return;    switch (e.id)      {      case ContainerEvent.COMPONENT_ADDED:        containerListener.componentAdded(e);        break;      case ContainerEvent.COMPONENT_REMOVED:        containerListener.componentRemoved(e);        break;      }  }  /**   * AWT 1.0 event processor.   *   * @param e The event that occurred.   *   * @deprecated use {@link #dispatchEvent(AWTEvent)} instead   */  public void deliverEvent(Event e)  {    if (!handleEvent (e))      {        synchronized (getTreeLock ())          {            Component parent = getParent ();            if (parent != null)              parent.deliverEvent (e);          }      }  }  /**   * Returns the component located at the specified point.  This is done   * by checking whether or not a child component claims to contain this   * point.  The first child component that does is returned.  If no   * child component claims the point, the container itself is returned,   * unless the point does not exist within this container, in which   * case <code>null</code> is returned.   *   * @param x The X coordinate of the point.   * @param y The Y coordinate of the point.   *   * @return The component containing the specified point, or   * <code>null</code> if there is no such point.   */  public Component getComponentAt(int x, int y)  {    return locate (x, y);  }  /**   * Returns the component located at the specified point.  This is done   * by checking whether or not a child component claims to contain this   * point.  The first child component that does is returned.  If no   * child component claims the point, the container itself is returned,   * unless the point does not exist within this container, in which   * case <code>null</code> is returned.   *   * @param x The x position of the point to return the component at.   * @param y The y position of the point to return the component at.   *   * @return The component containing the specified point, or <code>null</code>   * if there is no such point.   *   * @deprecated use {@link #getComponentAt(int, int)} instead   */  public Component locate(int x, int y)  {    synchronized (getTreeLock ())      {        if (!contains (x, y))          return null;        for (int i = 0; i < ncomponents; ++i)          {            // Ignore invisible children...            if (!component[i].isVisible ())              continue;            int x2 = x - component[i].x;            int y2 = y - component[i].y;            if (component[i].contains (x2, y2))              return component[i];          }        return this;      }  }  /**   * Returns the component located at the specified point.  This is done   * by checking whether or not a child component claims to contain this   * point.  The first child component that does is returned.  If no   * child component claims the point, the container itself is returned,   * unless the point does not exist within this container, in which   * case <code>null</code> is returned.   *   * @param p The point to return the component at.   * @return The component containing the specified point, or <code>null</code>   * if there is no such point.   */  public Component getComponentAt(Point p)  {    return getComponentAt (p.x, p.y);  }  public Component findComponentAt(int x, int y)  {    synchronized (getTreeLock ())      {        if (! contains(x, y))          return null;        for (int i = 0; i < ncomponents; ++i)          {            // Ignore invisible children...            if (!component[i].isVisible())              continue;            int x2 = x - component[i].x;            int y2 = y - component[i].y;            // We don't do the contains() check right away because            // findComponentAt would redundantly do it first thing.            if (component[i] instanceof Container)              {                Container k = (Container) component[i];                Component r = k.findComponentAt(x2, y2);                if (r != null)                  return r;              }            else if (component[i].contains(x2, y2))              return component[i];          }        return this;      }  }  public Component findComponentAt(Point p)  {    return findComponentAt(p.x, p.y);  }  /**   * Called when this container is added to another container to inform it   * to create its peer.  Peers for any child components will also be   * created.   */  public void addNotify()  {    super.addNotify();    addNotifyContainerChildren();  }  /**   * Called when this container is removed from its parent container to   * inform it to destroy its peer.  This causes the peers of all child   * component to be destroyed as well.   */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?