如何使 JLayeredPane() 和 JPanel() 透明?它总是显示超级窗口的背景
我怎样才能完全使这个灰色面板透明,以便我只能看到按钮“测试”,而看不到灰色框(JPanel或JLayeredPane)
屏幕截图:< img src="https://i.sstatic.net/Go57H.png" alt="在此处输入图像描述">
public class win extends JWindow
{
...
public win()
{
super(new JFrame());
layers = new JLayeredPane();
button = new JButton("close");
this.setLayout (new BorderLayout ());
..
button.setBackground(Color.RED);
button.setSize(200,200);
button.setLocation(0,20);
this.add("North", button);
JPanel p = new JPanel();
p.setOpaque(false);
p.setSize(300, 200);
p.setLocation(0, 0);
p.add(new JButton("Test"));
layers.add(p, new Integer(1));
layers.setSize(400,300);
layers.setLocation(400,50);
layers.setOpaque(false);
this.add("North", layers);
canvas.setSize(screenSize.width,screenSize.height);
this.add("North",canvas);
//com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.5f); // gives error in my Java version
}
}
后续: 按照建议安装,但还没有成功。
ERROR not solved: Exception in thread "main" java.lang.UnsupportedOperationException: The TRANSLUCENT translucency kind is not supported.
Installed:
compiz-gnome.i686 0:0.9.4-2.fc15
Dependency Installed:
compiz-gtk.i686 0:0.9.4-2.fc15 compiz-plugins-main.i686 0:0.9.4-1.fc15
libcompizconfig.i686 0:0.9.4-1.fc15 protobuf.i686 0:2.3.0-7.fc15
Complete!
You have mail in /var/spool/mail/root
[root@example ~]# xdpyinfo | grep -i render
RENDER
You have mail in /var/spool/mail/root
[root@example ~]# xdpyinfo | grep -i comp
Composite
XVideo-MotionCompensation
[root@example ~]#
How can i completely make this grey panel as transparent, so that i can see only the button "Test" but not the grey box (JPanel or JLayeredPane)
Screen shot:
public class win extends JWindow
{
...
public win()
{
super(new JFrame());
layers = new JLayeredPane();
button = new JButton("close");
this.setLayout (new BorderLayout ());
..
button.setBackground(Color.RED);
button.setSize(200,200);
button.setLocation(0,20);
this.add("North", button);
JPanel p = new JPanel();
p.setOpaque(false);
p.setSize(300, 200);
p.setLocation(0, 0);
p.add(new JButton("Test"));
layers.add(p, new Integer(1));
layers.setSize(400,300);
layers.setLocation(400,50);
layers.setOpaque(false);
this.add("North", layers);
canvas.setSize(screenSize.width,screenSize.height);
this.add("North",canvas);
//com.sun.awt.AWTUtilities.setWindowOpacity(this, 0.5f); // gives error in my Java version
}
}
Follow up: installed as recommended, but no luck yet.
ERROR not solved: Exception in thread "main" java.lang.UnsupportedOperationException: The TRANSLUCENT translucency kind is not supported.
Installed:
compiz-gnome.i686 0:0.9.4-2.fc15
Dependency Installed:
compiz-gtk.i686 0:0.9.4-2.fc15 compiz-plugins-main.i686 0:0.9.4-1.fc15
libcompizconfig.i686 0:0.9.4-1.fc15 protobuf.i686 0:2.3.0-7.fc15
Complete!
You have mail in /var/spool/mail/root
[root@example ~]# xdpyinfo | grep -i render
RENDER
You have mail in /var/spool/mail/root
[root@example ~]# xdpyinfo | grep -i comp
Composite
XVideo-MotionCompensation
[root@example ~]#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅本文或本文。请注意,并非所有环境都支持本文中描述的所有功能(半透明度、每像素透明度等)。
编辑:在我的系统(Ubuntu 10.04.2 LTS,Sun java 1.6.0_26)上,以下代码:
给出:
编辑2:灵感来自此讨论,我刚刚安装并配置了
compiz
,现在是“恒定不透明度”上面链接的第二篇文章中 Web Start 应用程序的“level”滑块突然可以移动到小于 100% 的值,并且演示框架实际上是半透明的。此外,上面显示的代码片段现在为所有三种半透明/透明度打印true
。并且AWTUtilities.setWindowOpacity(..)
不再抛出任何异常,而是生成一个透明窗口。See this article or this article. Note that not all environments support all the features (translucency, per pixel transparencyt etc.) described in the article.
EDIT: On my system (Ubuntu 10.04.2 LTS, Sun java 1.6.0_26) the following code:
gives:
EDIT2: Inspired by this discussion, I just installed and configured
compiz
and now the 'constant opacity level' slider of the web start application at the second article linked above suddenly can be moved to values smaller than 100% and the demo frame actually is translucent. Also the code snipped shown above now printstrue
for all three kinds of translucency/transparency. AndAWTUtilities.setWindowOpacity(..)
does not throw any more but produces a transparent window.