• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:JFrameを半透明化
#navi(../)
RIGHT:Posted by [[terai]] at 2010-06-14
*JFrameを半透明化 [#ea286453]
JFrameを半透明にします。

-&jnlp;
-&jar;
-&zip;

#screenshot

**サンプルコード [#l5dd57cc]
#code{{
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
//com.sun.awt.AWTUtilities.setWindowOpacity(frame, .5f);
com.sun.awt.AWTUtilities.setWindowOpaque(frame, false);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JPanel p = new JPanel();
p.add(new JButton("JButton"));
p.setBackground(new Color(.5f,.8f,.5f,.5f));
frame.getContentPane().add(p);
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}}


**解説 [#v5c228ee]
com.sun.awt.AWTUtilities.setWindowOpacity(frame, .5f);を使って半透明化すると、フレームのタイトルバーや、子コンポーネントまで半透明化されるので、代わりに上記のサンプルでは以下のようにして半透明化を行っています。
-JFrame.setDefaultLookAndFeelDecorated(true);で、タイトルパーなどをJRootPaneに描画
-com.sun.awt.AWTUtilities.setWindowOpaque(frame, false);でJFrameを完全に透明化
-ContentPaneにsetBackground(new Color(.5f,.8f,.5f,.5f));で半透明の背景色を設定したパネルを追加

**参考リンク [#pc13cea7]
-[[江戸の文様(和風素材・デスクトップ壁紙)>http://www.viva-edo.com/komon/edokomon.html]]
-[[How to Create Translucent and Shaped Windows>http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/]]

**コメント [#d57b3c09]
- そんな簡単にできるんですね!昔画面キャプチャしたり色々苦労した結果断念しました; -- [[riki]] &new{2010-06-14 (月) 22:57:11};

#comment