1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
import java.awt.*;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.plaf.basic.BasicLabelUI;
import sun.swing.SwingUtilities2;
/**
* Created by IntelliJ IDEA.
* User: bebe
* Date: 24-Jun-2006
* Time: 19:32:20
* To change this template use File | Settings | File Templates.
*/
public class MyLabelUI extends BasicLabelUI {
static {
labelUI = new MyLabelUI();
}
private static final Font labelFont = new Font("Verdana", Font.ITALIC, 20);
/* I tried to keep the same names as in BasicLabelUI. */
private static Rectangle paintIconR = new Rectangle();
private static Rectangle paintTextR = new Rectangle();
private static Rectangle paintViewR = new Rectangle();
private static Insets paintViewInsets = new Insets(10, 10, 10, 10);
private GradientPaint painterEnabled = null;
public MyLabelUI() {
super();
}
@Override
public Dimension getPreferredSize(JComponent c) {
JLabel label = (JLabel) c;
FontMetrics fm = c.getGraphics().getFontMetrics(labelFont);
return new Dimension(fm.stringWidth(label.getText()), fm.getHeight());
}
@Override
public void paint(Graphics g, JComponent c) {
JLabel label = (JLabel) c;
String text = label.getText();
Icon icon = (label.isEnabled()) ? label.getIcon() : label.getDisabledIcon();
if ((icon == null) && (text == null)) {
return;
}
Graphics2D g2d = (Graphics2D) g;
g2d.setFont(labelFont);
FontMetrics fm = g.getFontMetrics();
paintViewInsets = c.getInsets(paintViewInsets);
paintViewR.x = paintViewInsets.left;
paintViewR.y = paintViewInsets.top;
paintViewR.height = c.getHeight();
paintViewR.width = c.getWidth();
String clippedText = layoutCL(label, fm, text, icon, paintViewR, paintIconR, paintTextR);
if (icon != null) {
icon.paintIcon(c, g, paintIconR.x, paintIconR.y);
}
paintIconR.x = 0;
paintIconR.y = 0;
paintIconR.width = 0;
paintIconR.height = 0;
paintTextR.x = paintTextR.y = paintTextR.width = paintTextR.height = 0;
if (text != null) {
int textX = paintTextR.x;
int textY = paintTextR.y + fm.getAscent();
if (label.isEnabled()) {
if (painterEnabled == null) {
painterEnabled = new GradientPaint(paintViewR.x, paintViewR.y, Color.blue, 0, paintViewR.height, Color.green);
}
// fill the text with a nice gradient :-)
g2d.setPaint(painterEnabled);
/* paintEnabledText(label, g2d, clippedText, textX, textY);
I can't use this one so I need to do something dirty (use SwingUtilities2)
*/
int mnemIndex = label.getDisplayedMnemonicIndex();
SwingUtilities2.drawStringUnderlineCharAt(label, g2d, clippedText, mnemIndex, textX, textY);
// draw a kind of a border :-)
g2d.setColor(Color.BLUE);
g2d.draw3DRect(paintViewR.x, paintViewR.y, paintViewR.width - 1, paintViewR.height - 1, true);
} else {
paintDisabledText(label, g, clippedText, textX, textY);
g2d.setColor(Color.RED);
g2d.draw3DRect(paintViewR.x, paintViewR.y, paintViewR.width - 1, paintViewR.height - 1, true);
}
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame("MyLabel UI ");
frame.setSize(480, 300);
frame.setLayout(new FlowLayout());
JLabel label = new JLabel("Label rendered with my own UI (Enabled)!");
label.setUI(new MyLabelUI());
frame.add(label);
label = new JLabel("standard label (enabled)!");
frame.add(label);
label = new JLabel("Label rendered with my own UI (Disabled)!");
label.setEnabled(false);
label.setUI(new MyLabelUI());
frame.add(label);
label = new JLabel("standard label (disabled)!");
label.setEnabled(false);
frame.add(label);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
} |
Partager