⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 swingutilities.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
   */  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 || text.equals(""))      {        textIconGap = 0;	textR.width = 0;	textR.height = 0;      }    else      {        int fromIndex = 0;        textR.width = fm.stringWidth(text);        textR.height = fm.getHeight();         while (text.indexOf('\n', fromIndex) != -1)          {            textR.height += fm.getHeight();            fromIndex = text.indexOf('\n', fromIndex) + 1;          }      }    // 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                    : Math.max(iconR.height - textR.height, 0));        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:        break;      case BOTTOM:        verticalAdjustment += (viewR.height - u.height);        break;      case CENTER:        verticalAdjustment += ((viewR.height/2) - (u.height/2));        break;      }    switch (horizontalAlignment)      {      case LEFT:        break;      case RIGHT:        horizontalAdjustment += (viewR.width - u.width);        break;      case CENTER:        horizontalAdjustment += ((viewR.width/2) - (u.width/2));        break;      }    iconR.x += horizontalAdjustment;    iconR.y += verticalAdjustment;    textR.x += horizontalAdjustment;    textR.y += verticalAdjustment;    return text;  }  /**    * Calls {@link java.awt.EventQueue#invokeLater} with the   * specified {@link Runnable}.    */  public static void invokeLater(Runnable doRun)  {    java.awt.EventQueue.invokeLater(doRun);  }  /**    * Calls {@link java.awt.EventQueue#invokeAndWait} with the   * specified {@link Runnable}.    */  public static void invokeAndWait(Runnable doRun)    throws InterruptedException,    InvocationTargetException  {    java.awt.EventQueue.invokeAndWait(doRun);  }  /**    * Calls {@link java.awt.EventQueue#isDispatchThread()}.   *    * @return <code>true</code> if the current thread is the current AWT event    * dispatch thread.   */  public static boolean isEventDispatchThread()  {    return java.awt.EventQueue.isDispatchThread();  }    /**   * This method paints the given component at the given position and size.   * The component will be reparented to the container given.   *    * @param g The Graphics object to draw with.   * @param c The Component to draw   * @param p The Container to reparent to.   * @param x The x coordinate to draw at.   * @param y The y coordinate to draw at.   * @param w The width of the drawing area.   * @param h The height of the drawing area.   */  public static void paintComponent(Graphics g, Component c, Container p,                                     int x, int y, int w, int h)  {           Container parent = c.getParent();    if (parent != null)      parent.remove(c);    if (p != null)      p.add(c);        Shape savedClip = g.getClip();        g.setClip(x, y, w, h);    g.translate(x, y);    c.paint(g);        g.translate(-x, -y);    g.setClip(savedClip);  }  /**   * This method paints the given component in the given rectangle.   * The component will be reparented to the container given.   *    * @param g The Graphics object to draw with.   * @param c The Component to draw   * @param p The Container to reparent to.   * @param r The rectangle that describes the drawing area.   */    public static void paintComponent(Graphics g, Component c,                                     Container p, Rectangle r)  {    paintComponent(g, c, p, r.x, r.y, r.width, r.height);  }    /**   * This method returns the common Frame owner used in JDialogs or   * JWindow when no owner is provided.   *   * @return The common Frame    */  static Frame getOwnerFrame()  {    if (ownerFrame == null)      ownerFrame = new OwnerFrame();    return ownerFrame;  }  /**   * Checks if left mouse button was clicked.   *   * @param event the event to check   *   * @return true if left mouse was clicked, false otherwise.   */  public static boolean isLeftMouseButton(MouseEvent event)  {    return ((event.getModifiersEx() & InputEvent.BUTTON1_DOWN_MASK)	     == InputEvent.BUTTON1_DOWN_MASK);  }  /**   * Checks if middle mouse button was clicked.   *   * @param event the event to check   *   * @return true if middle mouse was clicked, false otherwise.   */

⌨️ 快捷键说明

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