container.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,606 行 · 第 1/4 页

JAVA
1,606
字号
    }    /**     * Removes all the components from this container.     * @see #add     * @see #remove     */    public void removeAll() {        synchronized (getTreeLock()) {            while (ncomponents > 0) {                Component comp = component[--ncomponents];                component[ncomponents] = null;                if (displayable) {                    comp.removeNotify();                }                if (layoutMgr != null) {                    layoutMgr.removeLayoutComponent(comp);                }                comp.parent = null;                if (containerListener != null ||                    (eventMask & AWTEvent.CONTAINER_EVENT_MASK) != 0) {                    ContainerEvent e = new ContainerEvent(this,                            ContainerEvent.COMPONENT_REMOVED,                            comp);                    dispatchEvent(e);                }            }            if (valid) {                invalidate();            }        }    }    /**     * Gets the layout manager for this container.     * @see #doLayout     * @see #setLayout     */    public LayoutManager getLayout() {        return layoutMgr;    }    /**     * Sets the layout manager for this container.     * @param mgr the specified layout manager     * @see #doLayout     * @see #getLayout     */    public void setLayout(LayoutManager mgr) {        layoutMgr = mgr;        if (valid) {            invalidate();        }    }    /**     * Causes this container to lay out its components.  Most programs     * should not call this method directly, but should invoke     * the <code>validate</code> method instead.     * @see java.awt.LayoutManager#layoutContainer     * @see #setLayout     * @see #validate     * @since JDK1.1     */    public void doLayout() {        LayoutManager layoutMgr = this.layoutMgr;        if (layoutMgr != null) {            layoutMgr.layoutContainer(this);        }    }    /**     * Invalidates the container.  The container and all parents     * above it are marked as needing to be laid out.  This method can     * be called often, so it needs to execute quickly.     * @see #validate     * @see #layout     * @see LayoutManager     */    public void invalidate() {        if (layoutMgr instanceof LayoutManager2) {            LayoutManager2 lm = (LayoutManager2) layoutMgr;            lm.invalidateLayout(this);        }        super.invalidate();    }    /**     * Validates this container and all of its subcomponents.     * <p>     * AWT uses <code>validate</code> to cause a container to lay out     * its subcomponents again after the components it contains     * have been added to or modified.     * @see #validate     * @see Component#invalidate     */    public void validate() {        /* Avoid grabbing lock unless really necessary. */        if (!valid) {            synchronized (getTreeLock()) {                if (!valid && displayable) {                    validateTree();                    valid = true;                }            }        }    }    /**     * Recursively descends the container tree and recomputes the     * layout for any subtrees marked as needing it (those marked as     * invalid).  Synchronization should be provided by the method     * that calls this one:  <code>validate</code>.     */    protected void validateTree() {        if (!valid) {            doLayout();            Component component[] = this.component;            for (int i = 0; i < ncomponents; ++i) {                Component comp = component[i];                if ((comp instanceof Container)                    && !(comp instanceof Window)                    && !comp.valid) {                    ((Container) comp).validateTree();                } else {                    comp.validate();                }            }        }        valid = true;    }    /**     * Recursively descends the container tree and invalidates all     * contained components.     */    void invalidateTree() {        synchronized (getTreeLock()) {            for (int i = 0; i < ncomponents; ++i) {                Component comp = component[i];                if (comp instanceof Container) {                    ((Container) comp).invalidateTree();                } else {                    if (comp.valid) {                        comp.invalidate();                    }                }            }            if (valid) {                invalidate();            }        }    }    /**     * Sets the font of this container.     * @param f The font to become this container's font.     * @see Component#getFont     * @since JDK1.0     */    public void setFont(Font f) {        boolean shouldinvalidate = false;        Font oldfont = getFont();        super.setFont(f);        Font newfont = getFont();        if (newfont != oldfont && (oldfont == null ||                !oldfont.equals(newfont))) {            invalidateTree();        }    }    /**     * Returns the preferred size of this container.     * @return    an instance of <code>Dimension</code> that represents     *                the preferred size of this container.     * @see       java.awt.Container#getMinimumSize     * @see       java.awt.Container#getLayout     * @see       java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)     * @see       java.awt.Component#getPreferredSize     */    public Dimension getPreferredSize() {        /* Avoid grabbing the lock if a reasonable cached size value         * is available.         */        Dimension dim = prefSize;        if (dim != null && isValid()) {            return dim;        }        synchronized (getTreeLock()) {            prefSize = (layoutMgr != null) ?                    layoutMgr.preferredLayoutSize(this) :                    super.getPreferredSize();            return prefSize;        }    }    /**     * Returns the minimum size of this container.     * @return    an instance of <code>Dimension</code> that represents     *                the minimum size of this container.     * @see       java.awt.Container#getPreferredSize     * @see       java.awt.Container#getLayout     * @see       java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)     * @see       java.awt.Component#getMinimumSize     * @since     JDK1.1     */    public Dimension getMinimumSize() {        /* Avoid grabbing the lock if a reasonable cached size value         * is available.         */        Dimension dim = minSize;        if (dim != null && isValid()) {            return dim;        }        synchronized (getTreeLock()) {            minSize = (layoutMgr != null) ?                    layoutMgr.minimumLayoutSize(this) :                    super.getMinimumSize();            return minSize;        }    }    /**     * Returns the maximum size of this container.     * @see #getPreferredSize     */    public Dimension getMaximumSize() {        /* Avoid grabbing the lock if a reasonable cached size value         * is available.         */        Dimension dim = maxSize;        if (dim != null && isValid()) {            return dim;        }        if (layoutMgr instanceof LayoutManager2) {            synchronized (getTreeLock()) {                LayoutManager2 lm = (LayoutManager2) layoutMgr;                maxSize = lm.maximumLayoutSize(this);            }        } else {            maxSize = super.getMaximumSize();        }        return maxSize;    }    /**     * Returns the alignment along the x axis.  This specifies how     * the component would like to be aligned relative to other     * components.  The value should be a number between 0 and 1     * where 0 represents alignment along the origin, 1 is aligned     * the furthest away from the origin, 0.5 is centered, etc.     */    public float getAlignmentX() {        float xAlign;        if (layoutMgr instanceof LayoutManager2) {            synchronized (getTreeLock()) {                LayoutManager2 lm = (LayoutManager2) layoutMgr;                xAlign = lm.getLayoutAlignmentX(this);            }        } else {            xAlign = super.getAlignmentX();        }        return xAlign;    }    /**     * Returns the alignment along the y axis.  This specifies how     * the component would like to be aligned relative to other     * components.  The value should be a number between 0 and 1     * where 0 represents alignment along the origin, 1 is aligned     * the furthest away from the origin, 0.5 is centered, etc.     */    public float getAlignmentY() {        float yAlign;        if (layoutMgr instanceof LayoutManager2) {            synchronized (getTreeLock()) {                LayoutManager2 lm = (LayoutManager2) layoutMgr;                yAlign = lm.getLayoutAlignmentY(this);            }        } else {            yAlign = super.getAlignmentY();        }        return yAlign;    }    /**     * Paints the container. This forwards the paint to any lightweight     * components that are children of this container. If this method is     * reimplemented, super.paint(g) should be called so that lightweight     * components are properly rendered. If a child component is entirely     * clipped by the current clipping setting in g, paint() will not be     * forwarded to that child.     *     * @param g the specified Graphics window     * @see   java.awt.Component#update(java.awt.Graphics)     */    public void paint(Graphics g) {        if (isShowing()) {            int ncomponents = this.ncomponents;            Component component[] = this.component;            Rectangle clip = g.getClipBounds();            for (int i = ncomponents - 1; i >= 0; i--) {                Component comp = component[i];                if (comp != null &&                    comp.visible &&                    comp.isLightweight()) {                    Rectangle cr = comp.getBounds();                    if ((clip == null) || cr.intersects(clip)) {                        Graphics cg = g.create();                        if (cg instanceof ConstrainableGraphics) {                            ((ConstrainableGraphics) cg).constrain(cr.x, cr.y, cr.width, cr.height);                        } else {                            cg.translate(cr.x, cr.y);                        }                        cg.clipRect(0, 0, cr.width, cr.height);                        cg.setFont(comp.getFont());                        cg.setColor(comp.getForeground());                        try {                            comp.paint(cg);                        } finally {                            cg.dispose();                        }                    }                }            }        }    }    /**     * Updates the container.  This forwards the update to any lightweight     * components that are children of this container.  If this method is     * reimplemented, super.update(g) should be called so that lightweight     * components are properly rendered.  If a child component is entirely     * clipped by the current clipping setting in g, update() will not be     * forwarded to that child.     *     * @param g the specified Graphics window     * @see   java.awt.Component#update(java.awt.Graphics)     */    public void update(Graphics g) {        if (isShowing()) {            paint(g);        }    }    /**     * Prints the container. This forwards the print to any lightweight     * components that are children of this container. If this method is     * reimplemented, super.print(g) should be called so that lightweight     * components are properly rendered. If a child component is entirely     * clipped by the current clipping setting in g, print() will not be     * forwarded to that child.     *     * @param g the specified Graphics window     * @see   java.awt.Component#update(java.awt.Graphics)     */    public void print(Graphics g) {        super.print(g);  // By default, Component.print() calls paint()        int ncomponents = this.ncomponents;        Component component[] = this.component;        Rectangle clip = g.getClipBounds();        for (int i = ncomponents - 1; i >= 0; i--) {            Component comp = component[i];            if (comp != null) {                Rectangle cr = comp.getBounds();                if ((clip == null) || cr.intersects(clip)) {                    Graphics cg = g.create(cr.x, cr.y, cr.width, cr.height);                    cg.setFont(comp.getFont());                    try {                        comp.print(cg);                    } finally {                        cg.dispose();                    }                }            }        }    }    /**     * Paints each of the components in this container.     * @param     g   the graphics context.     * @see       java.awt.Component#paint     * @see       java.awt.Component#paintAll     */    public void paintComponents(Graphics g) {        int ncomponents = this.ncomponents;        Component component[] = this.component;        for (int i = ncomponents - 1; i >= 0; i--) {            Component comp = component[i];            if (comp != null) {                Graphics cg = comp.getGraphics();                Rectangle parentRect = g.getClipBounds();                // Calculate the clipping region of the child's graphics                // context, by taking the intersection of the parent's                // clipRect (if any) and the child's bounds, and then                // translating it's coordinates to be relative to the child.                if (parentRect != null) {                    Rectangle childRect = comp.getBounds();                    if (childRect.intersects(parentRect) == false) {                        // Child component is completely clipped out: ignore.                        continue;                    }

⌨️ 快捷键说明

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