📄 jlayeredpane.java
字号:
* <p>The "back" of a layer is the first position drawn, so the component at * the "back" is usually the component which is occluded by the most * other components in its layer.</p> * * @param c the component to move to the back of its layer. * @throws IllegalArgumentException if the component is not a child of * this container. * @see #moveToFront */ public void moveToBack(Component c) { setPosition (c, -1); } /** * Return the position of a component within its layer. Positions are assigned * from the "front" (position 0) to the "back" (position N-1), and drawn from * the back towards the front. * * @param c the component to get the position of. * @throws IllegalArgumentException if the component is not a child of * this container. * @see #setPosition */ public int getPosition(Component c) { int layer = getLayer (c); int[] range = layerToRange(new Integer(layer)); int top = range[0]; int bot = range[1]; Component[] comps = getComponents (); for (int i = top; i < bot; ++i) { if (comps[i] == c) return i - top; } // should have found it throw new IllegalArgumentException (); } /** * Change the position of a component within its layer. Positions are assigned * 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. * @throws IllegalArgumentException if the component is not a child of * this container. * @see #getPosition */ public void setPosition(Component c, int position) { int layer = getLayer (c); int[] range = layerToRange(new Integer(layer)); if (range[0] == range[1]) throw new IllegalArgumentException (); int top = range[0]; int bot = range[1]; if (position == -1) position = (bot - top) - 1; int targ = Math.min(top + position, bot-1); int curr = -1; Component[] comps = getComponents(); for (int i = top; i < bot; ++i) { if (comps[i] == c) { curr = i; break; } } if (curr == -1) // should have found it throw new IllegalArgumentException(); super.swapComponents (curr, targ); revalidate(); repaint(); } /** * 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) { int[] range = layerToRange (getObjectForLayer (layer)); if (range[0] == range[1]) return new Component[0]; else { Component[] comps = getComponents (); int sz = range[1] - range[0]; Component[] nc = new Component[sz]; for (int i = 0; i < sz; ++i) nc[i] = comps[range[0] + i]; return nc; } } /** * 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) { int[] range = layerToRange (getObjectForLayer (layer)); if (range[0] == range[1]) return 0; else return (range[1] - range[0]); } /** * 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. * @throws IllegalArgumentException if the component is not a child of * this container. */ public int getIndexOf(Component c) { int layer = getLayer (c); int[] range = layerToRange(new Integer(layer)); Component[] comps = getComponents(); for (int i = range[0]; i < range[1]; ++i) { if (comps[i] == c) return i; } // should have found the component during iteration throw new IllegalArgumentException (); } /** * 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) { Integer lobj = getObjectForLayer (layer); if (! layers.containsKey(lobj)) layers.put (lobj, new Integer (0)); int[] range = layerToRange (lobj); if (range[0] == range[1]) return range[0]; int top = range[0]; int bot = range[1]; if (position == -1 || position > (bot - top)) return bot; else return top + position; } /** * 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); int layer = getLayer(c); decrLayer(new Integer(layer)); componentToLayer.remove(c); super.remove(index); // FIXME: Figure out if this call is correct. revalidate(); } /** * <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) { componentToLayer.put (c, getObjectForLayer (layer)); } /** * 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) { remove(c); add(c, getObjectForLayer (layer)); setPosition(c, position); revalidate(); repaint(); } /** * 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). * * @param comp the component to add. * @param layerConstraint an integer specifying the layer to add the component to. * @param index an ignored parameter, for compatibility. */ protected void addImpl(Component comp, Object layerConstraint, int index) { Integer layer; if (layerConstraint != null && layerConstraint instanceof Integer) layer = (Integer) layerConstraint; else if (componentToLayer.containsKey (comp)) layer = (Integer) componentToLayer.remove (comp); else layer = DEFAULT_LAYER; int newIdx = insertIndexForLayer(layer.intValue (), index); componentToLayer.put (comp, layer); incrLayer (layer); super.addImpl(comp, null, 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) { getLayeredPaneAbove(component).setLayer(component, 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); } /** * Overridden to return <code>false</code>, since <code>JLayeredPane</code> * cannot guarantee that its children don't overlap. * * @return <code>false</code> */ public boolean isOptimizedDrawingEnabled() { return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -