TITLE:Look and Feelの変更

Look and Feelの変更

編集者:Terai Atsuhiro
作成日:2003-10-22
更新日:2023-01-31 (火) 10:49:04

概要

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

  • metal

    #screenshot(,metal.png)

  • motif

    #screenshot(,motif.png)

  • windows

    #screenshot(,windows.png)

サンプルコード

//%JAVA_HOME%/demo/jfc/SwingSet2/src/SwingSet2.java

// 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);
  }
}
  • &jnlp;
  • &jar;
  • &zip;

解説

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

Look and Feelを切り替えて、いろんなコンポーネントの見栄えを比較したい場合は、SwingSet2*1 が手軽で分かりやすいと思います。


切り替えではなく、ネイティブの Look and Feel に変更したい場合は、UIManager.getSystemLookAndFeelClassName()で実装クラスの名前を取得し、これを設定してやります。

try{
  //UIManager.getInstalledLookAndFeels();
  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exception e) {e.printStackTrace();}

Ubuntu(GNOME) で、上記のようにネイティブL&F を指定すると、NullPointerException が発生する場合は、直前にUIManager.getInstalledLookAndFeels()を呼んでおくと回避できるようです。


オプションでデフォルトのLook and Feelを変更する場合は以下のように指定します。

java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel App

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

参考リンク

コメント