Swing/MenuWithShadow のバックアップソース(No.12)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- バックアップ を表示
- Swing/MenuWithShadow へ行く。
- 1 (2006-10-23 (月) 14:11:21)
- 2 (2006-10-23 (月) 15:24:20)
- 3 (2006-11-10 (金) 13:50:24)
- 4 (2006-12-02 (土) 02:34:36)
- 5 (2007-03-13 (火) 00:25:08)
- 6 (2007-03-28 (水) 21:07:41)
- 7 (2007-05-18 (金) 15:18:46)
- 8 (2008-05-29 (木) 16:11:48)
- 9 (2010-11-26 (金) 14:09:24)
- 10 (2012-01-27 (金) 17:50:13)
- 11 (2013-02-22 (金) 18:58:01)
- 12 (2014-06-10 (火) 18:59:02)
- 13 (2014-09-19 (金) 16:48:53)
- 14 (2014-10-25 (土) 23:43:48)
- 15 (2014-11-12 (水) 01:59:23)
- 16 (2015-02-27 (金) 12:02:52)
- 17 (2016-11-24 (木) 15:40:54)
- 18 (2017-11-28 (火) 13:53:48)
- 19 (2018-09-26 (水) 13:51:55)
- 20 (2019-05-22 (水) 19:35:38)
- 21 (2020-04-08 (水) 16:08:40)
- 22 (2021-10-16 (土) 00:34:28)
TITLE:Menuに半透明の影を付ける #navi(../) #tags(JPopupMenu, UIManager, Robot, Translucent) RIGHT:Posted by &author(aterai); at 2006-10-23 * Menuに半透明の影を付ける [#u32fd4be] メニューに半透明の影を付けます。 #download #ref(https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTPz0ZEG6I/AAAAAAAAAek/pd0ErBB9eBg/s800/MenuWithShadow.png) ** サンプルコード [#eb189a65] #code(link){{ public class CustomPopupMenuUI extends BasicPopupMenuUI { public static ComponentUI createUI(JComponent c) { return new CustomPopupMenuUI(); } private static boolean isHeavyWeightContainer(Component contents) { for(Container p=contents.getParent();p!=null;p=p.getParent()) { if((p instanceof JWindow) || (p instanceof Panel)) { return true; } } return false; } public Popup getPopup(JPopupMenu popup, int x, int y) { Popup pp = super.getPopup(popup,x,y); JPanel panel = (JPanel)popup.getParent(); if(isHeavyWeightContainer(panel)) { System.out.println("outer"); Point p = new Point(x,y); panel.setBorder(new ShadowBorder(panel,p)); }else{ System.out.println("inner"); panel.setBorder(new ShadowBorderInPanel()); } panel.setOpaque(false); return pp; } //...... }} ** 解説 [#d73e76f4] 上記のサンプルでは、`BasicPopupMenuUI`を継承した、`CustomPopupMenuUI`を作成して、これを`UIManager`に登録しています。 #code{{ UIManager.put("PopupMenuUI","example.CustomPopupMenuUI"); }} この`CustomPopupMenuUI`では、ポップアップメニューがフレームの中にあるか外にあるかで、影のつけ方を切り替えています。外にある場合は、`Robot`を使って画面をキャプチャしています。 %%メニューがフレームの外にはみ出す場合に、メニューをすばやく切り替えたりすると、ゴミが残ることがあるようです。参考リンクの`contrib.com.jgoodies.looks.common.ShadowPopupFactory`を使っても同様のゴミが出る場合があります。%% 再現しなくなった?ようです。 ---- `contrib.com.jgoodies.looks.common.ShadowPopupFactory`を使用しても、同様の影を作成することができます。http://www.jgoodies.com/ などからダウンロードして、以下のように設定してみてください。 #code{{ //UIManager.put("PopupMenuUI","example.CustomPopupMenuUI"); contrib.com.jgoodies.looks.common.ShadowPopupFactory.install(); }} ---- `Web Start`で起動してフレームの外側にメニューが表示される場合は、`java.security.AccessControlException: access denied (java.awt.AWTPermission createRobot)`が発生します。 ** 参考リンク [#w4e129d8] - [http://www.oreilly.co.jp/books/4873112788/toc.html Java Swing Hacks #11 ドロップシャドウ付きのメニューを作る] - [https://substance.dev.java.net/ substance: Substance Java look and feel - main page] -- contrib.com.jgoodies.looks.common.ShadowPopupFactory - [[JPopupMenuに半透明の影を付ける>Swing/DropShadowPopup]] ** コメント [#r700cff6] - ポップアップメニューがフレーム内にあるかどうかではなく、`HeavyWeightContainer`かどうかで影のつけ方を切り替えるように変更。 -- [[aterai]] &new{2008-05-29 (木) 16:13:30}; #comment