swingutilities.java
来自「Mac OS X 10.4.9 for x86 Source Code gcc」· Java 代码 · 共 1,323 行 · 第 1/3 页
JAVA
1,323 行
Container c = (Container) parent; return c.findComponentAt(x, y); } /** * Converts a point from a component's local coordinate space to "screen" * coordinates (such as the coordinate space mouse events are delivered * in). This operation is equivalent to translating the point by the * location of the component (which is the origin of its coordinate * space). * * @param p The point to convert * @param c The component which the point is expressed in terms of * * @see convertPointFromScreen */ public static void convertPointToScreen(Point p, Component c) { Point c0 = c.getLocationOnScreen(); p.translate(c0.x, c0.y); } /** * Converts a point from "screen" coordinates (such as the coordinate * space mouse events are delivered in) to a component's local coordinate * space. This operation is equivalent to translating the point by the * negation of the component's location (which is the origin of its * coordinate space). * * @param p The point to convert * @param c The component which the point should be expressed in terms of */ public static void convertPointFromScreen(Point p, Component c) { Point c0 = c.getLocationOnScreen(); p.translate(-c0.x, -c0.y); } /** * Converts a point <code>(x,y)</code> from the coordinate space of one * component to another. This is equivalent to converting the point from * <code>source</code> space to screen space, then back from screen space * to <code>destination</code> space. If exactly one of the two * Components is <code>null</code>, it is taken to refer to the root * ancestor of the other component. If both are <code>null</code>, no * transformation is done. * * @param source The component which the point is expressed in terms of * @param x Horizontal coordinate of point to transform * @param y Vertical coordinate of point to transform * @param destination The component which the return value will be * expressed in terms of * * @return The point <code>(x,y)</code> converted from the coordinate space of the * source component to the coordinate space of the destination component * * @see #convertPointToScreen * @see #convertPointFromScreen * @see #convertRectangle * @see #getRoot */ public static Point convertPoint(Component source, int x, int y, Component destination) { Point pt = new Point(x, y); if (source == null && destination == null) return pt; if (source == null) source = getRoot(destination); if (destination == null) destination = getRoot(source); convertPointToScreen(pt, source); convertPointFromScreen(pt, destination); return pt; } public static Point convertPoint(Component source, Point aPoint, Component destination) { return convertPoint(source, aPoint.x, aPoint.y, destination); } /** * Converts a rectangle from the coordinate space of one component to * another. This is equivalent to converting the rectangle from * <code>source</code> space to screen space, then back from screen space * to <code>destination</code> space. If exactly one of the two * Components is <code>null</code>, it is taken to refer to the root * ancestor of the other component. If both are <code>null</code>, no * transformation is done. * * @param source The component which the rectangle is expressed in terms of * @param rect The rectangle to convert * @param destination The component which the return value will be * expressed in terms of * * @return A new rectangle, equal in size to the input rectangle, but * with its position converted from the coordinate space of the source * component to the coordinate space of the destination component * * @see #convertPointToScreen * @see #convertPointFromScreen * @see #convertPoint * @see #getRoot */ public static Rectangle convertRectangle(Component source, Rectangle rect, Component destination) { Point pt = convertPoint(source, rect.x, rect.y, destination); return new Rectangle(pt.x, pt.y, rect.width, rect.height); } /** * Convert a mouse event which refrers to one component to another. This * includes changing the mouse event's coordinate space, as well as the * source property of the event. If <code>source</code> is * <code>null</code>, it is taken to refer to <code>destination</code>'s * root component. If <code>destination</code> is <code>null</code>, the * new event will remain expressed in <code>source</code>'s coordinate * system. * * @param source The component the mouse event currently refers to * @param sourceEvent The mouse event to convert * @param destination The component the new mouse event should refer to * * @return A new mouse event expressed in terms of the destination * component's coordinate space, and with the destination component as * its source * * @see #convertPoint */ public static MouseEvent convertMouseEvent(Component source, MouseEvent sourceEvent, Component destination) { Point newpt = convertPoint(source, sourceEvent.getX(), sourceEvent.getY(), destination); return new MouseEvent(destination, sourceEvent.getID(), sourceEvent.getWhen(), sourceEvent.getModifiersEx(), newpt.x, newpt.y, sourceEvent.getClickCount(), sourceEvent.isPopupTrigger(), sourceEvent.getButton()); } /** * Recursively walk the component tree under <code>comp</code> calling * <code>updateUI</code> on each {@link JComponent} found. This causes * the entire tree to re-initialize its UI delegates. * * @param comp The component to walk the children of, calling <code>updateUI</code> */ public static void updateComponentTreeUI(Component comp) { if (comp == null) return; if (comp instanceof Container) { Component[] children = ((Container)comp).getComponents(); for (int i = 0; i < children.length; ++i) updateComponentTreeUI(children[i]); } if (comp instanceof JComponent) ((JComponent)comp).updateUI(); } /** * <p>Layout a "compound label" consisting of a text string and an icon * which is to be placed near the rendered text. Once the text and icon * are laid out, the text rectangle and icon rectangle parameters are * altered to store the calculated positions.</p> * * <p>The size of the text is calculated from the provided font metrics * object. This object should be the metrics of the font you intend to * paint the label with.</p> * * <p>The position values control where the text is placed relative to * the icon. The horizontal position value should be one of the constants * <code>LEADING</code>, <code>TRAILING</code>, <code>LEFT</code>, * <code>RIGHT</code> or <code>CENTER</code>. The vertical position value * should be one fo the constants <code>TOP</code>, <code>BOTTOM</code> * or <code>CENTER</code>.</p> * * <p>The text-icon gap value controls the number of pixels between the * icon and the text.</p> * * <p>The alignment values control where the text and icon are placed, as * a combined unit, within the view rectangle. The horizontal alignment * value should be one of the constants <code>LEADING</code>, * <code>TRAILING</code>, <code>LEFT</code>, <code>RIGHT</code> or * <code>CENTER</code>. The vertical alignment valus should be one of the * constants <code>TOP</code>, <code>BOTTOM</code> or * <code>CENTER</code>.</p> * * <p>If the <code>LEADING</code> or <code>TRAILING</code> constants are * given for horizontal alignment or horizontal text position, they are * interpreted relative to the provided component's orientation property, * a constant in the {@link java.awt.ComponentOrientation} class. For * example, if the component's orientation is <code>LEFT_TO_RIGHT</code>, * then the <code>LEADING</code> value is a synonym for <code>LEFT</code> * and the <code>TRAILING</code> value is a synonym for * <code>RIGHT</code></p> * * <p>If the text and icon are equal to or larger than the view * rectangle, the horizontal and vertical alignment values have no * affect.</p> * * @param c A component used for its orientation value * @param fm The font metrics used to measure the text * @param text The text to place in the compound label * @param icon The icon to place next to the text * @param verticalAlignment The vertical alignment of the label relative * to its component * @param horizontalAlignment The horizontal alignment of the label * relative to its component * @param verticalTextPosition The vertical position of the label's text * relative to its icon * @param horizontalTextPosition The horizontal position of the label's * text relative to its icon * @param viewR The view rectangle, specifying the area which layout is * constrained to * @param iconR A rectangle which is modified to hold the laid-out * position of the icon * @param textR A rectangle which is modified to hold the laid-out * position of the text * @param textIconGap The distance between text and icon * * @return The string of characters, possibly truncated with an elipsis, * which is laid out in this label */ public static String layoutCompoundLabel(JComponent c, FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap) { // Fix up the orientation-based horizontal positions. if (horizontalTextPosition == LEADING) { if (c.getComponentOrientation() == ComponentOrientation.RIGHT_TO_LEFT) horizontalTextPosition = RIGHT; else horizontalTextPosition = LEFT; } else if (horizontalTextPosition == TRAILING) { if (c.getComponentOrientation() == ComponentOrientation.RIGHT_TO_LEFT) horizontalTextPosition = LEFT; else horizontalTextPosition = RIGHT; } // Fix up the orientation-based alignments. if (horizontalAlignment == LEADING) { if (c.getComponentOrientation() == ComponentOrientation.RIGHT_TO_LEFT) horizontalAlignment = RIGHT; else horizontalAlignment = LEFT; } else if (horizontalAlignment == TRAILING) { if (c.getComponentOrientation() == ComponentOrientation.RIGHT_TO_LEFT) horizontalAlignment = LEFT; else horizontalAlignment = RIGHT; } return layoutCompoundLabel(fm, text, icon, verticalAlignment, horizontalAlignment, verticalTextPosition, horizontalTextPosition, viewR, iconR, textR, textIconGap); } /** * <p>Layout a "compound label" consisting of a text string and an icon * which is to be placed near the rendered text. Once the text and icon * are laid out, the text rectangle and icon rectangle parameters are * altered to store the calculated positions.</p> * * <p>The size of the text is calculated from the provided font metrics * object. This object should be the metrics of the font you intend to * paint the label with.</p> * * <p>The position values control where the text is placed relative to * the icon. The horizontal position value should be one of the constants * <code>LEFT</code>, <code>RIGHT</code> or <code>CENTER</code>. The * vertical position value should be one fo the constants * <code>TOP</code>, <code>BOTTOM</code> or <code>CENTER</code>.</p> * * <p>The text-icon gap value controls the number of pixels between the * icon and the text.</p> * * <p>The alignment values control where the text and icon are placed, as * a combined unit, within the view rectangle. The horizontal alignment * value should be one of the constants <code>LEFT</code>, <code>RIGHT</code> or * <code>CENTER</code>. The vertical alignment valus should be one of the * constants <code>TOP</code>, <code>BOTTOM</code> or * <code>CENTER</code>.</p> * * <p>If the text and icon are equal to or larger than the view * rectangle, the horizontal and vertical alignment values have no * affect.</p> * * <p>Note that this method does <em>not</em> know how to deal with * horizontal alignments or positions given as <code>LEADING</code> or * <code>TRAILING</code> values. Use the other overloaded variant of this * method if you wish to use such values. * * @param fm The font metrics used to measure the text * @param text The text to place in the compound label * @param icon The icon to place next to the text * @param verticalAlignment The vertical alignment of the label relative * to its component * @param horizontalAlignment The horizontal alignment of the label * relative to its component * @param verticalTextPosition The vertical position of the label's text * relative to its icon * @param horizontalTextPosition The horizontal position of the label's * text relative to its icon * @param viewR The view rectangle, specifying the area which layout is * constrained to * @param iconR A rectangle which is modified to hold the laid-out * position of the icon * @param textR A rectangle which is modified to hold the laid-out * position of the text * @param textIconGap The distance between text and icon * * @return The string of characters, possibly truncated with an elipsis, * which is laid out in this label */ public static String layoutCompoundLabel(FontMetrics fm, String text, Icon icon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewR, Rectangle iconR, Rectangle textR, int textIconGap) { // Work out basic height and width. if (icon == null) { textIconGap = 0; iconR.width = 0; iconR.height = 0; } else { iconR.width = icon.getIconWidth(); iconR.height = icon.getIconHeight(); } if (text == null) { textIconGap = 0; textR.width = 0; textR.height = 0; } else { textR.width = fm.stringWidth(text); textR.height = fm.getHeight(); } // Work out the position of text and icon, assuming the top-left coord // starts at (0,0). We will fix that up momentarily, after these // "position" decisions are made and we look at alignment. switch (horizontalTextPosition) { case LEFT: textR.x = 0; iconR.x = textR.width + textIconGap; break; case RIGHT: iconR.x = 0; textR.x = iconR.width + textIconGap; break; case CENTER: int centerLine = Math.max(textR.width, iconR.width) / 2; textR.x = centerLine - textR.width/2; iconR.x = centerLine - iconR.width/2; break; } switch (verticalTextPosition) { case TOP: textR.y = 0; iconR.y = (horizontalTextPosition == CENTER ? textR.height + textIconGap : 0); break; case BOTTOM: iconR.y = 0; textR.y = (horizontalTextPosition == CENTER ? iconR.height + textIconGap : iconR.height - textR.height); break; case CENTER: int centerLine = Math.max(textR.height, iconR.height) / 2; textR.y = centerLine - textR.height/2; iconR.y = centerLine - iconR.height/2; break; } // The two rectangles are laid out correctly now, but only assuming // that their upper left corner is at (0,0). If we have any alignment other // than TOP and LEFT, we need to adjust them. Rectangle u = textR.union(iconR); int horizontalAdjustment = viewR.x; int verticalAdjustment = viewR.y; switch (verticalAlignment) { case TOP:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?