トップ 差分 一覧 ソース 置換 検索 ヘルプ PDF RSS ログイン

SwingのLook and Feel を自作

http://blog.livedoor.jp/lalha_java/archives/50747986.html

サンプル1

MyLookAndFeel.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
    import javax.swing.plaf.basic.BasicLookAndFeel;

    public class MyLookAndFeel extends BasicLookAndFeel {
      public String getDescription() {
        return "This is My Look & Feel.";
      }

      public String getID() {
        return "MyLookAndFeel";
      }

      public String getName() {
        return "My Look & Feel";
      }

      public boolean isNativeLookAndFeel() {
        return false;
      }

      public boolean isSupportedLookAndFeel() {
        return true;
      }
    }

LookAndFeelTest.java

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
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

public class LookAndFeelTest {
  public static void main(String[] args) {
    try {
      UIManager.setLookAndFeel(new MyLookAndFeel());
    } catch (Exception e) {
    }
  
    JFrame frame = new JFrame("Look & Feel サンプル");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    
    JTextField textField = new JTextField(10);
    JButton button = new JButton("検索");

    frame.getContentPane().add(panel);

    panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.add(Box.createHorizontalGlue());
    panel.add(textField);
    panel.add(Box.createHorizontalStrut(5));
    panel.add(button);
    panel.add(Box.createHorizontalGlue());
    
    frame.pack();
    frame.setVisible(true);
  }
}


サンプル2

MyTextFieldUI.java

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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTextFieldUI;

public class MyTextFieldUI extends BasicTextFieldUI {
  private static final Color GRADATION_START =
      new Color(255, 255, 255, 0);
  private static final Color GRADATION_END =
      new Color(0, 0, 0, 64);

  private final JComponent comp;
  
  public MyTextFieldUI(JComponent comp) {
    if (comp == null) {
      throw new IllegalArgumentException(
        "comp should not be null.");
    }
    this.comp = comp;
  }
  
  public static ComponentUI createUI(JComponent c) {
    return new MyTextFieldUI(c);
  }

  protected void paintBackground(Graphics g) {
    super.paintBackground(g);
    Dimension size = comp.getSize();
    MyLookAndFeelUtil.gradationFill(
        GRADATION_START, GRADATION_END,
        0, 0, size.width, size.height,
        g);
  }
}

MyButtonUI.java

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
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicButtonUI;

public class MyButtonUI extends BasicButtonUI {
  private static final Color GRADATION_START =
      new Color(255, 255, 255, 0);
  private static final Color GRADATION_END =
      new Color(0, 0, 0, 64);

  public static ComponentUI createUI(JComponent c) {
    return new MyButtonUI();
  }

  public void paint(Graphics g, JComponent c) {
    super.paint(g, c);
    MyLookAndFeelUtil.gradationFill(
        GRADATION_START, GRADATION_END,
        0, 0, c.getWidth(), c.getHeight(),
        g);
  }
}

MyLookAndFeelUtil.java

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
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Rectangle;

public class MyLookAndFeelUtil {
  public static void gradationFill(
      Color c1, Color c2,
      int x, int y, int width, int height,
      Graphics g) {
    Paint p = new GradientPaint(
      (float)x, (float)y, c1,
      (float)x, (float)(y + height), c2);
    fill(p, x, y, width, height, g);
  }

  public static void fill(
      Paint p,
      int x, int y, int width, int height,
      Graphics g) {
    Graphics2D g2d = (Graphics2D)g;
    g2d.setPaint(p);
    g2d.fill(new Rectangle(x, y, width, height));
  }
}

MyLookAndFeel.java

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
import javax.swing.UIDefaults;
import javax.swing.plaf.basic.BasicLookAndFeel;

public class MyLookAndFeel extends BasicLookAndFeel {
  public String getDescription() {
    return "This is My Look & Feel.";
  }

  public String getID() {
    return "MyLookAndFeel";
  }

  public String getName() {
    return "My Look & Feel";
  }

  public boolean isNativeLookAndFeel() {
    return false;
  }

  public boolean isSupportedLookAndFeel() {
    return true;
  }

  public UIDefaults getDefaults() {
    UIDefaults uiDefaults = super.getDefaults();
    
    uiDefaults.putDefaults(new Object[] {
      "ButtonUI", "MyButtonUI",
      "TextFieldUI", "MyTextFieldUI",
    });
    
    return uiDefaults;
  }
}
[カテゴリ: プログラミング言語 > Java]



  • Hatenaブックマークに追加
  • livedoorクリップに追加
  • del.icio.usに追加
  • FC2ブックマークに追加

最終更新時間:2011年11月12日 21時41分18秒