public void paintComponent(Graphics g) { Icon icon = getIcon(); FontMetrics fm = getFontMetrics(getFont()); Rectangle viewRect = new Rectangle(getSize()); JBInsets.removeFrom(viewRect, getInsets()); Rectangle iconRect = new Rectangle(); Rectangle textRect = new Rectangle(); String text = SwingUtilities.layoutCompoundLabel( this, fm, getText(), icon, SwingConstants.CENTER, horizontalTextAlignment(), SwingConstants.CENTER, horizontalTextPosition(), viewRect, iconRect, textRect, iconTextSpace()); ActionButtonLook look = ActionButtonLook.IDEA_LOOK; look.paintBackground(g, this); look.paintIconAt(g, this, icon, iconRect.x, iconRect.y); look.paintBorder(g, this); UISettings.setupAntialiasing(g); g.setColor(isButtonEnabled() ? getForeground() : getInactiveTextColor()); SwingUtilities2.drawStringUnderlineCharAt( this, g, text, getMnemonicCharIndex(text), textRect.x, textRect.y + fm.getAscent()); }
public Dimension getPreferredSize() { Dimension basicSize = super.getPreferredSize(); Icon icon = getIcon(); FontMetrics fm = getFontMetrics(getFont()); Rectangle viewRect = new Rectangle(0, 0, Short.MAX_VALUE, Short.MAX_VALUE); Insets insets = getInsets(); int dx = insets.left + insets.right; int dy = insets.top + insets.bottom; Rectangle iconR = new Rectangle(); Rectangle textR = new Rectangle(); SwingUtilities.layoutCompoundLabel( this, fm, getText(), icon, SwingConstants.CENTER, horizontalTextAlignment(), SwingConstants.CENTER, horizontalTextPosition(), viewRect, iconR, textR, iconTextSpace()); int x1 = Math.min(iconR.x, textR.x); int x2 = Math.max(iconR.x + iconR.width, textR.x + textR.width); int y1 = Math.min(iconR.y, textR.y); int y2 = Math.max(iconR.y + iconR.height, textR.y + textR.height); Dimension rv = new Dimension(x2 - x1 + dx, y2 - y1 + dy); rv.width += Math.max(basicSize.height - rv.height, 0); rv.width = Math.max(rv.width, basicSize.width); rv.height = Math.max(rv.height, basicSize.height); return rv; }