1 package net.sf.openrocket.gui.components;
5 import javax.swing.JLabel;
6 import javax.swing.SwingConstants;
9 * A resizeable and styleable JLabel. The method {@link #resizeFont(float)} changes the
10 * current font size by the given (positive or negative) amount. The change is relative
11 * to the current font size. The method {@link #setFontStyle(Style)} sets the style
12 * (bold/italic) of the font.
14 * A nice small text is achievable by <code>new ResizeLabel("My text", -2);</code>
16 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
19 public class StyledLabel extends JLabel {
25 BOLD_ITALIC(Font.BOLD | Font.ITALIC);
28 Style(int fontStyle) {
29 this.style = fontStyle;
31 public int getFontStyle() {
38 public StyledLabel() {
39 this("", SwingConstants.LEADING, 0f);
42 public StyledLabel(String text) {
43 this(text, SwingConstants.LEADING, 0f);
46 public StyledLabel(float size) {
47 this("", SwingConstants.LEADING, size);
50 public StyledLabel(String text, float size) {
51 this(text, SwingConstants.LEADING, size);
54 public StyledLabel(String text, int horizontalAlignment, float size) {
55 super(text, horizontalAlignment);
57 checkPreferredSize(size, Style.PLAIN);
62 public StyledLabel(Style style) {
63 this("", SwingConstants.LEADING, 0f, style);
66 public StyledLabel(String text, Style style) {
67 this(text, SwingConstants.LEADING, 0f, style);
70 public StyledLabel(float size, Style style) {
71 this("", SwingConstants.LEADING, size, style);
74 public StyledLabel(String text, float size, Style style) {
75 this(text, SwingConstants.LEADING, size, style);
78 public StyledLabel(String text, int horizontalAlignment, float size, Style style) {
79 super(text, horizontalAlignment);
82 checkPreferredSize(size, style);
88 private void checkPreferredSize(float size, Style style) {
89 String str = this.getText();
90 if (str.startsWith("<html>") && str.indexOf("<br") < 0) {
91 StyledLabel label = new StyledLabel("plaintext", size, style);
93 System.out.println("Plain-text label: " + label.getPreferredSize());
94 System.out.println("HTML label: " + this.getPreferredSize());
100 public void resizeFont(float size) {
101 Font font = this.getFont();
102 font = font.deriveFont(font.getSize2D()+size);
106 public void setFontStyle(Style style) {
107 Font font = this.getFont();
108 font = font.deriveFont(style.getFontStyle());