BorderFactoryクラス

広告

今まではBorderの各クラスからオブジェクトを作成していましたが、BorderFactoryクラスというものが用意され、BorderFactoryクラスの各メソッドを使って、様々なBorderを作成できるようになっています。要はFactory Methodとして使えるようになっています。

まずはBorderFactoryのクラス図を見て下さい。

java.lang.Object
  javax.swing.BorderFactory

public class BorderFactory
extends Object

今までは下記のように記述していました。

JButton btn = new JButton("test");
btn.setBorder(new LineBorder(Color.black));

BorderFactoryクラスを使うと下記のような記述方法になります。

JButton btn = new JButton("test");
btn.setBorder(BorderFactory.createLineBorder(Color.black));

ではBorderFactoryクラスで用意されている各メソッドをご紹介しておきます。

static Border createBevelBorder(int type)
static Border createBevelBorder(int type, Color highlight, Color shadow)
static Border createBevelBorder(int type, Color highlightOuter, 
    Color highlightInner, Color shadowOuter, Color shadowInner)
static CompoundBorder createCompoundBorder()
static CompoundBorder createCompoundBorder(Border outsideBorder, Border insideBorder)
static Border createEmptyBorder()
static Border createEmptyBorder(int top, int left, int bottom, int right)
static Border createEtchedBorder()
static Border createEtchedBorder(Color highlight, Color shadow)
static Border createEtchedBorder(int type)
static Border createEtchedBorder(int type, Color highlight, Color shadow)
static Border createLineBorder(Color color)
static Border createLineBorder(Color color, int thickness)
static Border createLoweredBevelBorder()
static MatteBorder createMatteBorder(int top, int left, int bottom, 
    int right, Color color)
static MatteBorder createMatteBorder(int top, int left, int bottom, 
    int right, Icon tileIcon)
static Border createRaisedBevelBorder()
static TitledBorder createTitledBorder(Border border)
static TitledBorder createTitledBorder(Border border, String title)
static TitledBorder createTitledBorder(Border border, String title, 
    int titleJustification, int titlePosition)
static TitledBorder createTitledBorder(Border border, String title, 
    int titleJustification, int titlePosition, Font titleFont)
static TitledBorder createTitledBorder(Border border, String title, 
    int titleJustification, int titlePosition, Font titleFont, Color titleColor)
static TitledBorder createTitledBorder(String title)

これらのメソッドを使うことで、各Borderクラスをnew演算子を使って直接作成しなくても、Borderを作成できるようになります。Sunのサンプルなどを見ていると、BorderFactoryクラスを使った書き方をされているようです。

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。