📄 container.java
字号:
component[--ncomponents] = null; invalidate(); if (layoutMgr != null) layoutMgr.removeLayoutComponent(r); r.parent = null; if (isShowing ()) { // Post event to notify of removing the component. ContainerEvent ce = new ContainerEvent(this, ContainerEvent.COMPONENT_REMOVED, r); getToolkit().getSystemEventQueue().postEvent(ce); // Repaint this container. repaint(); } } } /** * Removes the specified component from this container. * * @param comp 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() { layout (); } /** * Layout the components in this container. * * @deprecated use {@link #doLayout()} instead */ public void layout() { if (layoutMgr != null) layoutMgr.layoutContainer (this); } /** * Invalidates this container to indicate that it (and all parent * containers) need to be laid out. */ public void invalidate() { super.invalidate(); if (layoutMgr != null && layoutMgr instanceof LayoutManager2) { LayoutManager2 lm2 = (LayoutManager2) layoutMgr; lm2.invalidateLayout(this); } } /** * 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() { super.invalidate(); // Clean cached layout state. for (int i = 0; i < ncomponents; i++) { Component comp = component[i]; comp.invalidate(); if (comp instanceof Container) ((Container) comp).invalidateTree(); } if (layoutMgr != null && layoutMgr instanceof LayoutManager2) { LayoutManager2 lm2 = (LayoutManager2) layoutMgr; lm2.invalidateLayout(this); } } /** * 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) { if( (f != null && (font == null || !font.equals(f))) || f == null) { 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() { synchronized(treeLock) { if(valid && prefSize != null) return new Dimension(prefSize); LayoutManager layout = getLayout(); if (layout != null) { Dimension layoutSize = layout.preferredLayoutSize(this); if(valid) prefSize = layoutSize; return new Dimension(layoutSize); } 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(valid && minSize != null) return new Dimension(minSize); LayoutManager layout = getLayout(); if (layout != null) { minSize = layout.minimumLayoutSize (this); return minSize; } else return super.minimumSize (); } /** * Returns the maximum size of this container. * * @return The maximum size of this container. */ public Dimension getMaximumSize() { if (valid && maxSize != null) return new Dimension(maxSize); LayoutManager layout = getLayout(); if (layout != null && layout instanceof LayoutManager2) { LayoutManager2 lm2 = (LayoutManager2) layout; maxSize = lm2.maximumLayoutSize(this); return maxSize; } 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() { LayoutManager layout = getLayout(); float alignmentX = 0.0F; if (layout != null && layout instanceof LayoutManager2) { LayoutManager2 lm2 = (LayoutManager2) layout; alignmentX = lm2.getLayoutAlignmentX(this); } else alignmentX = super.getAlignmentX(); return alignmentX; } /** * 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() { LayoutManager layout = getLayout(); float alignmentY = 0.0F; if (layout != null && layout instanceof LayoutManager2) { LayoutManager2 lm2 = (LayoutManager2) layout; alignmentY = lm2.getLayoutAlignmentY(this); } else alignmentY = super.getAlignmentY(); return alignmentY; } /** * 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; // 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. * * @specnote The specification suggests that this method forwards the * update() call to all its lightweight children. Tests show * that this is not done either in the JDK. The exact behaviour * seems to be that the background is cleared in heavyweight * Containers, and all other containers * directly call paint(), causing the (lightweight) children to * be painted. */ public void update(Graphics g) { // It seems that the JDK clears the background of containers like Panel // and Window (within this method) but not of 'plain' Containers or // JComponents. This could // lead to the assumption that it only clears heavyweight containers. // However that is not quite true. In a test with a custom Container // that overrides isLightweight() to return false, the background is // also not cleared. So we do a check on !(peer instanceof LightweightPeer) // instead. ComponentPeer p = peer; if (p != null && !(p instanceof LightweightPeer)) g.clearRect(0, 0, getWidth(), getHeight()); paint(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) { 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -