TITLE:Look and Feelの変更
#navi(../)
*Look and Feelの変更 [#a86ced2d]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2003-10-22~
更新日:&lastmod;

#contents

**概要 [#rb2d3f29]
アプリケーションのLook and Feelを変更します。以下のサンプルコードは、''%JAVA_HOME%/demo/jfc/SwingSet2/src/SwingSet2.java'' から引用改変したものです。

#twocolumn
-metal
>
#screenshot(,metal.png)

-motif
>
#screenshot(,motif.png)
#twocolumn
-windows
>
#screenshot(,windows.png)
#twocolumn

**サンプルコード [#d19635f9]
#code{{
// Possible Look and Feels
static String mac     = "com.sun.java.swing.plaf.mac.MacLookAndFeel";
static String metal   = "javax.swing.plaf.metal.MetalLookAndFeel";
static String motif   = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
static String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
static String gtk     = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";

// The current Look and Feel
private static String currentLookAndFeel = metal;
private ButtonGroup lafMenuGroup = new ButtonGroup();
public JMenuItem createLafMenuItem(JMenu menu, String label,
                                   String mnemonic,
                                   String accessibleDescription,
                                   String laf) {
  JMenuItem mi=(JRadioButtonMenuItem)menu.add(new JRadioButtonMenuItem(label));
  lafMenuGroup.add(mi);
  mi.addActionListener(new ChangeLookAndFeelAction(this, laf));
  mi.setEnabled(isAvailableLookAndFeel(laf));
  return mi;
}

protected boolean isAvailableLookAndFeel(String laf) {
  try{
    Class lnfClass = Class.forName(laf);
    LookAndFeel newLAF = (LookAndFeel)(lnfClass.newInstance());
    return newLAF.isSupportedLookAndFeel();
  }catch(Exception e) {
    return false;
  }
}

class ChangeLookAndFeelAction extends AbstractAction{
  AbstractMenuPanel swingset;
  String laf;
  protected ChangeLookAndFeelAction(AbstractMenuPanel swingset, String laf) {
    super("ChangeTheme");
    this.swingset = swingset;
    this.laf = laf;
  }
  public void actionPerformed(ActionEvent e) {
    swingset.setLookAndFeel(laf);
  }
}

public void setLookAndFeel(String laf) {
  if(currentLookAndFeel != laf) {
    currentLookAndFeel = laf;
    updateLookAndFeel();
  }
}

public void updateLookAndFeel() {
  try{
    UIManager.setLookAndFeel(currentLookAndFeel);
    SwingUtilities.updateComponentTreeUI(this);
    // update LAF for the toplevel frame, too
    SwingUtilities.updateComponentTreeUI(getFrame());
    //SwingUtilities.updateComponentTreeUI(popupMenu);
  }catch(Exception ex) {
    System.out.println("Failed loading LnF: " + currentLookAndFeel);
    System.out.println(ex);
  }
}
}}
#code{{
JMenu lafMenu = new JMenu("Laf");
JMenuItem mi = createLafMenuItem(
  lafMenu,
  "LafMenu.java_label",
  "LafMenu.java_mnemonic",
  "LafMenu.java_accessible_description",
  metal);
mi.setSelected(true); //this is the default LnF
createLafMenuItem(
  lafMenu,
  "LafMenu.mac_label",
  "LafMenu.mac_mnemonic",
  "LafMenu.mac_accessible_description",
  mac);
createLafMenuItem(
  lafMenu,
  "LafMenu.motif_label",
  "LafMenu.motif_mnemonic",
  "LafMenu.motif_accessible_description",
  motif);
createLafMenuItem(
  lafMenu,
  "LafMenu.windows_label",
  "LafMenu.windows_mnemonic",
  "LafMenu.windows_accessible_description",
  windows);
createLafMenuItem(lafMenu,
  "LafMenu.gtk_label",
  "LafMenu.gtk_mnemonic",
  "LafMenu.gtk_accessible_description",
  gtk);
}}
-&jnlp;
-&jar;
-&zip;

**解説 [#hf60bcb1]
サンプルでは、メニューバーから、metal、motif、windowsなどのLookAndFeelを切り替えることができます。

//Windows2000、java version "1.4.2"などで、GTKが選択できてしまう場合があるようです。このとき、Look and Feelを別のものに戻した場合、Backgroundが再描画されなくなります。1.4.2_02以上にすれば問題ないようです。

Look and Feelを切り替えて、いろんなコンポーネントの見栄えを比較したい場合は、SwingSet2((''%JAVA_HOME%\demo\jfc\SwingSet2\SwingSet2.jar'')) が手軽で分かりやすいと思います。

切り替えではなく、ネイティブの Look and Feel に変更したい場合は、UIManager.getSystemLookAndFeelClassName()で実装クラスの名前を取得し、これを設定してやります。
#code{{
try{
  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  //SwingUtilities.updateComponentTreeUI(this);
}catch(Exception e) {}
}}

オプションでデフォルトのLook and Feelを変更する場合は以下のように指定します。
 java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel App

%JAVA_HOME%\lib以下に''swing.properties''を作ってデフォルトのLook and Feelを指定する方法もあります。

**参考リンク [#w6ef8eac]
-[[How to Set the Look and Feel>http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/plaf.html]]

**コメント [#v06b763b]
#comment