jlayeredpane.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 690 行 · 第 1/2 页
JAVA
690 行
* from the "front" (position 0) to the "back" (position N-1), and drawn from * the back towards the front. * * @param c the component to change the position of * @param position the position to assign the component to * * @see #getPosition */ public void setPosition(Component c, int position) { int layer = getLayer(c); int index = insertIndexForLayer(layer, position); setComponentZOrder(c, index); } /** * Return an array of all components within a layer of this * container. Components are ordered front-to-back, with the "front" * element (which draws last) at position 0 of the returned array. * * @param layer the layer to return components from * * @return the components in the layer */ public Component[] getComponentsInLayer(int layer) { Component[] inLayer = new Component[getComponentCountInLayer(layer)]; Component[] components = getComponents(); int j = 0; for (int i = 0; i < components.length; ++i) { if (layer == getLayer(components[i])) { inLayer[j] = components[i]; j++; } } return inLayer; } /** * Return the number of components within a layer of this * container. * * @param layer the layer count components in * * @return the number of components in the layer */ public int getComponentCountInLayer(int layer) { Component[] components = getComponents(); int count = 0; for (int i = components.length - 1; i >= 0; --i) { if (getLayer(components[i]) == layer) count++; } return count; } /** * Return a hashtable mapping child components of this container to * Integer objects representing the component's layer assignments. */ protected Hashtable getComponentToLayer() { return componentToLayer; } /** * Return the index of a component within the underlying (contiguous) * array of children. This is a "raw" number which does not represent the * child's position in a layer, but rather its position in the logical * drawing order of all children of the container. * * @param c the component to look up. * * @return the external index of the component or <code>-1</code> if * <code>c</code> is not a child of this layered pane */ public int getIndexOf(Component c) { return getComponentZOrder(c); } /** * Return an Integer object which holds the same int value as the * parameter. This is strictly an optimization to minimize the number of * identical Integer objects which we allocate. * * @param layer the layer number as an int. * * @return the layer number as an Integer, possibly shared. */ protected Integer getObjectForLayer(int layer) { switch (layer) { case -30000: return FRAME_CONTENT_LAYER; case 0: return DEFAULT_LAYER; case 100: return PALETTE_LAYER; case 200: return MODAL_LAYER; case 300: return POPUP_LAYER; case 400: return DRAG_LAYER; default: break; } return new Integer(layer); } /** * Computes an index at which to request the superclass {@link * java.awt.Container} inserts a component, given an abstract layer and * position number. * * @param layer the layer in which to insert a component. * @param position the position in the layer at which to insert a component. * * @return the index at which to insert the component. */ protected int insertIndexForLayer(int layer, int position) { // position < 0 means insert at greatest position within layer. if (position < 0) position = Integer.MAX_VALUE; Component[] components = getComponents(); int index = 0; // Try to find the start index of the specified layer. int p = -1; for (int i = 0; i < components.length; i++) { int l = getLayer(components[i]); if (l > layer) index++; // If we are in the layer we look for, try to find the position. else if (l == layer) { p++; if (p < position) index++; else break; } // No need to look further if the layer at i is smaller than layer. else break; } return index; } /** * Removes a child from this container. The child is specified by * index. After removal, the child no longer occupies a layer. * * @param index the index of the child component to remove. */ public void remove(int index) { Component c = getComponent(index); if (! (c instanceof JComponent)) componentToLayer.remove(c); super.remove(index); } /** * Removes all components from this container. * * @since 1.5 */ public void removeAll() { componentToLayer.clear(); super.removeAll(); } /** * <p>Set the layer property for a component, within this container. The * component will be implicitly mapped to the bottom-most position in the * layer, but only if added <em>after</em> calling this method.</p> * * <p>Read that carefully: this method should be called <em>before</em> the * component is added to the container.</p> * * @param c the component to set the layer property for. * @param layer the layer number to assign to the component. */ public void setLayer(Component c, int layer) { setLayer(c, layer, -1); } /** * Set the layer and position of a component, within this container. * * @param c the child component to set the layer property for. * @param layer the layer number to assign to the component. * @param position the position number to assign to the component. */ public void setLayer(Component c, int layer, int position) { Integer layerObj = getObjectForLayer(layer); if (c instanceof JComponent) { JComponent jc = (JComponent) c; jc.putClientProperty(LAYER_PROPERTY, layerObj); } else componentToLayer.put (c, layerObj); // Set position only of component is already added to this layered pane. if (getIndexOf(c) != -1) setPosition(c, position); } /** * Overrides the default implementation from {@link java.awt.Container} * such that <code>layerConstraint</code> is interpreted as an {@link * Integer}, specifying the layer to which the component will be added * (at the bottom position). * * The argument <code>index</code> specifies the position within the layer * at which the component should be added, where <code>0</code> is the top * position greater values specify positions below that and <code>-1</code> * specifies the bottom position. * * @param comp the component to add * @param layerConstraint an integer specifying the layer to add the * component to * @param index the position within the layer */ protected void addImpl(Component comp, Object layerConstraint, int index) { int layer; if (layerConstraint != null && layerConstraint instanceof Integer) layer = ((Integer) layerConstraint).intValue(); else layer = getLayer(comp); int newIdx = insertIndexForLayer(layer, index); setLayer(comp, layer); super.addImpl(comp, layerConstraint, newIdx); } /** * Sets the layer property for a JComponent. * * @param component the component for which to set the layer * @param layer the layer property to set */ public static void putLayer(JComponent component, int layer) { component.putClientProperty(LAYER_PROPERTY, new Integer(layer)); } /** * Returns the accessible context for this <code>JLayeredPane</code>. * * @return the accessible context for this <code>JLayeredPane</code> */ public AccessibleContext getAccessibleContext() { if (accessibleContext == null) accessibleContext = new AccessibleJLayeredPane(); return accessibleContext; } /** * This method is overridden order to provide a reasonable painting * mechanism for <code>JLayeredPane</code>. This is necessary since * <code>JLayeredPane</code>'s do not have an own UI delegate. * * Basically this method clears the background for the * <code>JLayeredPane</code> and then calls <code>super.paint(g)</code>. * * @param g the graphics context to use */ public void paint(Graphics g) { if (isOpaque()) { Color oldColor = g.getColor(); Rectangle clip = g.getClipBounds(); g.setColor(getBackground()); g.fillRect(clip.x, clip.y, clip.width, clip.height); g.setColor(oldColor); } super.paint(g); } /** * Returns <code>false</code> if components in this layered pane can overlap, * otherwise <code>true</code>. * * @return <code>false</code> if components in this layered pane can overlap, * otherwise <code>true</code> */ public boolean isOptimizedDrawingEnabled() { int numChildren = getComponentCount(); boolean result = true; for (int i = 0; i < numChildren; ++i) { Component c1 = getComponent(i); if (! c1.isVisible()) continue; Rectangle r1 = c1.getBounds(); if (r1.isEmpty()) continue; for (int j = i + 1; j < numChildren; ++j) { Component c2 = getComponent(j); if (! c2.isVisible()) continue; Rectangle r2 = c2.getBounds(); if (r2.isEmpty()) continue; if (r1.intersects(r2)) { result = false; break; } if (result == false) break; } } return result; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?