Swing/WindowOpacity のバックアップの現在との差分(No.15)
-
category: swing
folder: WindowOpacity
title: JFrameを半透明化
tags: [JFrame, Translucent, Transparent, JRootPane, ContentPane, TexturePaint]
author: aterai
pubdate: 2010-06-14T14:13:21+09:00
description: JFrameを半透明にします。
description: JFrameのタイトルや子コンポーネントを除く背景が半透明になるよう設定します。
image:
hreflang:
href: https://java-swing-tips.blogspot.com/2010/06/translucent-jframe.html lang: en
概要
JFrame
を半透明にします。
概要
JFrame
タイトルや子コンポーネントを除く背景が半透明になるよう設定します。
Screenshot
Advertisement
サンプルコード
サンプルコード
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
#spandel
//com.sun.awt.AWTUtilities.setWindowOpacity(frame, .5f);
#spanend
#spandel
com.sun.awt.AWTUtilities.setWindowOpaque(frame, false);
#spanend
#spanadd
// com.sun.awt.AWTUtilities.setWindowOpacity(frame, .5f);
#spanend
#spanadd
// com.sun.awt.AWTUtilities.setWindowOpaque(frame, false); // Java 6
#spanend
#spanadd
frame.setBackground(new Color(0x0, true));
#spanend
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
#spanadd
#spanend
JPanel p = new JPanel();
p.add(new JButton("JButton"));
#spandel
p.setBackground(new Color(.5f,.8f,.5f,.5f));
#spanend
#spanadd
p.setBackground(new Color(.5f, .8f, .5f, .5f));
#spanend
#spanadd
#spanend
frame.getContentPane().add(p);
frame.setSize(320, 240);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
View in GitHub: Java, Kotlin解説
com.sun.awt.AWTUtilities.setWindowOpacity(frame, .5f);
を使って半透明化すると、フレームのタイトルバーや、子コンポーネントまで半透明化されるので、代わりに上記のサンプルでは以下のようにして半透明化を行っています。
解説
com.sun.awt.AWTUtilities.setWindowOpacity(frame, .5f);
を使ってWindow
を半透明化するとJFrame
などのタイトルバーや子コンポーネントまで半透明化されるので、代わりに上記のサンプルでは以下のようにして半透明化を行っています。
-
JFrame.setDefaultLookAndFeelDecorated(true);
で、タイトルバーなどをJRootPane
に描画 -
JFrame.setDefaultLookAndFeelDecorated(true);
でタイトルバーなどをJRootPane
に描画 com.sun.awt.AWTUtilities.setWindowOpaque(frame, false);
でJFrame
を完全に透明化-
JDK 1.7.0
の場合は、代わりに、frame.setBackground(new Color(0,0,0,0));
-
-
ContentPane
にsetBackground(new Color(.5f,.8f,.5f,.5f));
で半透明の背景色を設定したパネルを追加-
JDK 1.7.0
の場合は、代わりにframe.setBackground(new Color(0x0, true));
-
-
ContentPane
にsetBackground(new Color(.5f, .8f, .5f, .5f));
で半透明の背景色を設定したパネルを追加
参考リンク
参考リンク
- 江戸の文様(和風素材・デスクトップ壁紙)
- How to Create Translucent and Shaped Windows (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
- How to Create Translucent and Shaped Windows (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features)
- Robotを使用してスクリーンショットを取得する