Swing/LookAndFeel のバックアップの現在との差分(No.8)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/LookAndFeel へ行く。
- 1 (2007-11-01 (木) 13:25:44)
- 2 (2007-11-30 (金) 14:02:09)
- 3 (2008-03-19 (水) 16:12:01)
- 4 (2008-03-19 (水) 19:39:39)
- 5 (2008-03-28 (金) 21:31:38)
- 6 (2008-03-30 (日) 06:51:55)
- 7 (2008-03-31 (月) 18:41:49)
- 8 (2008-07-08 (火) 15:02:28)
- 9 (2009-01-19 (月) 13:19:19)
- 10 (2009-01-21 (水) 13:53:40)
- 11 (2010-07-16 (金) 15:30:30)
- 12 (2011-03-16 (水) 20:02:25)
- 13 (2011-03-17 (木) 11:18:56)
- 14 (2011-12-11 (日) 16:01:55)
- 15 (2012-06-17 (日) 15:28:44)
- 16 (2012-10-27 (土) 05:02:28)
- 17 (2013-02-26 (火) 14:48:07)
- 18 (2013-02-26 (火) 15:50:30)
- 19 (2013-08-18 (日) 01:05:45)
- 20 (2014-02-12 (水) 19:01:12)
- 21 (2014-11-01 (土) 00:46:09)
- 22 (2014-12-03 (水) 03:09:44)
- 23 (2015-01-03 (土) 06:16:15)
- 24 (2016-05-27 (金) 15:28:49)
- 25 (2016-05-31 (火) 14:28:16)
- 26 (2017-04-04 (火) 14:17:08)
- 27 (2017-09-05 (火) 16:04:48)
- 28 (2017-11-02 (木) 15:34:40)
- 29 (2018-12-27 (木) 16:36:30)
- 30 (2019-05-22 (水) 19:35:38)
- 31 (2020-11-22 (日) 01:08:44)
- 32 (2022-08-20 (土) 22:15:25)
- 33 (2023-01-31 (火) 10:49:04)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:Look and Feelの変更 #navi(../) *Look and Feelの変更 [#a86ced2d] Posted by [[terai]] at 2003-10-22 --- category: swing folder: LookAndFeel title: Look and Feelの変更 tags: [LookAndFeel, JMenuBar] author: aterai pubdate: 2003-11-24 description: メニューバーから選択したLook and Feelを起動中のアプリケーションに適用します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTPf78s81I/AAAAAAAAAeE/DIOTnqtAOnY/s800/LookAndFeel.png --- * 概要 [#summary] メニューバーから選択した`Look and Feel`を起動中のアプリケーションに適用します。 #contents #download(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTPf78s81I/AAAAAAAAAeE/DIOTnqtAOnY/s800/LookAndFeel.png) **概要 [#rb2d3f29] アプリケーションのLook and Feelを変更します。以下のサンプルコードは、''%JAVA_HOME%/demo/jfc/SwingSet2/src/SwingSet2.java'' から引用改変したものです。 -&jnlp; -&jar; -&zip; #screenshot **サンプルコード [#d19635f9] #code{{ //%JAVA_HOME%/demo/jfc/SwingSet2/src/SwingSet2.java * サンプルコード [#sourcecode] #code(link){{ // %JAVA_HOME%/demo/jfc/SwingSet2/src/SwingSet2.java // Possible Look & Feels private static final String mac = "com.sun.java.swing.plaf.mac.MacLookAndFeel"; private static final String metal = "javax.swing.plaf.metal.MetalLookAndFeel"; private static final String motif = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; private static final String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; private static final String gtk = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; private static final String nimbus = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"; private static final String MAC = "com.sun.java.swing.plaf.mac.MacLookAndFeel"; private static final String METAL = "javax.swing.plaf.metal.MetalLookAndFeel"; private static final String MOTIF = "com.sun.java.swing.plaf.motif.MotifLookAndFeel"; private static final String WINDOWS = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel"; private static final String GTK = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel"; // JDK 1.6.0_10 // private static final String NIMBUS = "com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel"; // JDK 1.7.0 private static final String NIMBUS = "javax.swing.plaf.nimbus.NimbusLookAndFeel"; // The current Look & Feel private static String currentLookAndFeel = metal; private static String currentLookAndFeel = METAL; private final ButtonGroup lafMenuGroup = new ButtonGroup(); public JMenuItem createLafMenuItem(JMenu menu, String label, String laf) { JMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(label)); JMenuItem mi = menu.add(new JRadioButtonMenuItem(label)); lafMenuGroup.add(mi); mi.addActionListener(new ChangeLookAndFeelAction(laf)); mi.setEnabled(isAvailableLookAndFeel(laf)); return mi; } protected static boolean isAvailableLookAndFeel(String laf) { try{ try { Class lnfClass = Class.forName(laf); LookAndFeel newLAF = (LookAndFeel)(lnfClass.newInstance()); LookAndFeel newLAF = (LookAndFeel) lnfClass.newInstance(); return newLAF.isSupportedLookAndFeel(); }catch(Exception e) { } catch (Exception e) { return false; } } }} #code{{ private class ChangeLookAndFeelAction extends AbstractAction{ private String laf; // ... private class ChangeLookAndFeelAction extends AbstractAction { private final String laf; protected ChangeLookAndFeelAction(String laf) { super("ChangeTheme"); this.laf = laf; } public void actionPerformed(ActionEvent e) { @Override public void actionPerformed(ActionEvent e) { setLookAndFeel(laf); } } private void setLookAndFeel(String laf) { if(currentLookAndFeel != laf) { currentLookAndFeel = laf; try{ UIManager.setLookAndFeel(currentLookAndFeel); SwingUtilities.updateComponentTreeUI(frame); }catch(Exception ex) { ex.printStackTrace(); System.out.println("Failed loading L&F: " + currentLookAndFeel); } if (currentLookAndFeel.equals(laf)) return; currentLookAndFeel = laf; try { UIManager.setLookAndFeel(currentLookAndFeel); SwingUtilities.updateComponentTreeUI(frame); } catch (Exception ex) { ex.printStackTrace(); System.out.println("Failed loading L&F: " + currentLookAndFeel); } } }} **解説 [#hf60bcb1] サンプルでは、メニューバーから、metal、motif、windowsなどのLookAndFeelを切り替えることができます。 * 解説 [#explanation] 上記のサンプルでは、`metal`、`motif`、`windows`などの`LookAndFeel`を予め用意しておき、`isAvailableLookAndFeel(...)`メソッドでそれらが実行した環境で使用できるかを調べてメニューの選択可・不可を変更しています。 //Windows2000、java version "1.4.2"などで、GTKが選択できてしまう場合があるようです。このとき、Look and Feelを別のものに戻した場合、Backgroundが再描画されなくなります。1.4.2_02以上にすれば問題ないようです。 - `Look and Feel`を切り替えて各種コンポーネントの表示などを比較したい場合は`%JAVA_HOME%/demo/jfc/SwingSet2/SwingSet2.jar`が便利 - `SwingSet3`では`UIManager.getInstalledLookAndFeels()`メソッドを使って利用可能な`LookAndFeel`の一覧を取得しこれをメニューに表示している -- [[LookAndFeelの一覧を取得する>Swing/InstalledLookAndFeels]] - `java.exe`などのコマンドオプションでデフォルトの`Look and Feel`を変更する場合は以下のように指定する java.exe -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel App Look and Feelを切り替えて、いろんなコンポーネントの見栄えを比較したい場合は、SwingSet2((''%JAVA_HOME%\demo\jfc\SwingSet2\SwingSet2.jar'')) が手軽で分かりやすいと思います。 ---- 切り替えではなく、ネイティブの Look and Feel に変更したい場合は、UIManager.getSystemLookAndFeelClassName()で実装クラスの名前を取得し、これを設定してやります。 - `%JAVA_HOME%/lib`以下に`swing.properties`を作ってデフォルトの`Look and Feel`を指定する方法もある -- [https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html#properties Specifying the Look and Feel: swing.properties File] - `SystemLookAndFeel`を使用する場合は`UIManager.getSystemLookAndFeelClassName()`でその実装クラス名を取得して設定する #code{{ try{ //UIManager.getInstalledLookAndFeels(); try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); }catch(Exception e) {e.printStackTrace();} } catch (Exception e) { e.printStackTrace(); } }} Ubuntu(GNOME) で、上記のようにネイティブL&F を指定すると、NullPointerException が発生する場合は、直前にUIManager.getInstalledLookAndFeels()を呼んでおくと回避できるようです。 -[[JDK1.6 で使うと JDK 側のバグ (6389282) に引っかかってしまうのか NPE>http://blogs.sun.com/katakai/entry/omegat_in_mdi_mode]] -[[Java6 の痛いバグ… NetBeans デスクトップアプリが Linux で動かず… - Masaki Katakai's Weblog>http://blogs.sun.com/katakai/entry/bad_issue_for_swing_gtk]] - %%`Ubuntu(GNOME)`で上記のようにシステム`LookAndFeel`を指定すると`NullPointerException`が発生する場合は、直前に`UIManager.getInstalledLookAndFeels()`を呼んでおくと回避可能%% `6u10`で修正済 -- [https://bugs.openjdk.org/browse/JDK-6389282 Bug ID: 6389282 NPE in GTKLookAndFeel.initSystemColorDefaults() on mustang when remote X11 displaying.] // -- [http://blogs.sun.com/katakai/entry/omegat_in_mdi_mode JDK1.6 で使うと JDK 側のバグ (6389282) に引っかかってしまうのか NPE] // -- [http://blogs.sun.com/katakai/entry/bad_issue_for_swing_gtk Java6 の痛いバグ… NetBeans デスクトップアプリが Linux で動かず… - Masaki Katakai's Weblog] ---- オプションでデフォルトのLook and Feelを変更する場合は以下のように指定します。 java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel App * 参考リンク [#reference] - `%JAVA_HOME%/demo/jfc/SwingSet2/src/SwingSet2.java` - [https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html How to Set the Look and Feel] - [[LookAndFeelの一覧を取得する>Swing/InstalledLookAndFeels]] -- `SwingSet3`からコードを引用したサンプル %JAVA_HOME%\lib以下に''swing.properties''を作ってデフォルトのLook and Feelを指定する方法もあります。 * コメント [#comment] #comment - スクリーンショットなどを更新。 -- &user(aterai); &new{2008-03-28 (金) 21:37:52}; **参考リンク [#w6ef8eac] -[[How to Set the Look and Feel>http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/plaf.html]] **コメント [#v06b763b] - %%Nimbusを追加、%% スクリーンショットなどを更新。 -- [[terai]] &new{2008-03-28 (金) 21:37:52}; #comment