Java tutorial
//package com.java2s; import java.awt.Rectangle; import javax.swing.Icon; import javax.swing.JComponent; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; public class Main { public static String layoutComponentCenter(JComponent c, String text, Icon icon, int iconTextGap, Rectangle viewRect, Rectangle iconRect, Rectangle textRect) { int center = SwingConstants.CENTER; return layoutComponent(c, text, icon, iconTextGap, center, center, center, center, viewRect, iconRect, textRect); } public static String layoutComponent(JComponent c, String text, Icon icon, int iconTextGap, int verticalAlignment, int horizontalAlignment, int verticalTextAlignment, int horizontalTextAlignment, Rectangle viewRect, Rectangle iconRect, Rectangle textRect) { resetRectangles(iconRect, textRect); return SwingUtilities.layoutCompoundLabel(c, c.getFontMetrics(c.getFont()), text, icon, verticalAlignment, horizontalAlignment, verticalTextAlignment, horizontalTextAlignment, viewRect, iconRect, textRect, iconTextGap); } /** * Resets the specified {@code Rectangle}'s properties to 0. * * @param rects the rectangles to reset */ public static void resetRectangles(Rectangle... rects) { for (Rectangle r : rects) { r.x = r.y = r.width = r.height = 0; } } }