如何制作透明的JLayeredPane()和JPanel()?(How to make transparent JLayeredPane() and JPanel()? It always shows

编程入门 行业动态 更新时间:2024-10-28 01:13:21
如何制作透明的JLayeredPane()和JPanel()?(How to make transparent JLayeredPane() and JPanel()? It always shows background of super window)

如何让这个灰色面板完全透明 ,这样我只能看到按钮“Test”而不是灰色框(JPanel或JLayeredPane)

截屏:

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 ~]#

最满意答案

请参阅此文章或本文 。 请注意,并非所有环境都支持本文中描述的所有功能(半透明,每像素透明度等)。

编辑 :在我的系统(Ubuntu 10.04.2 LTS,Sun java 1.6.0_26)上面的代码如下:

System.out.println("TRANSLUCENT supported: " + AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT)); System.out.println("PERPIXEL_TRANSPARENT supported: " + AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.PERPIXEL_TRANSPARENT)); System.out.println("PERPIXEL_TRANSLUCENT supported: " + AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.PERPIXEL_TRANSLUCENT));

得到:

TRANSLUCENT supported: false PERPIXEL_TRANSPARENT supported: true PERPIXEL_TRANSLUCENT supported: true

EDIT2:受到这个讨论的启发,我刚刚安装并配置了compiz ,现在突然可以将上面链接的第二篇文章中的Web启动应用程序的“常量不透明度级别”滑块移动到小于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:

System.out.println("TRANSLUCENT supported: " + AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.TRANSLUCENT)); System.out.println("PERPIXEL_TRANSPARENT supported: " + AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.PERPIXEL_TRANSPARENT)); System.out.println("PERPIXEL_TRANSLUCENT supported: " + AWTUtilities.isTranslucencySupported(AWTUtilities.Translucency.PERPIXEL_TRANSLUCENT));

gives:

TRANSLUCENT supported: false PERPIXEL_TRANSPARENT supported: true PERPIXEL_TRANSLUCENT supported: true

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 prints true for all three kinds of translucency/transparency. And AWTUtilities.setWindowOpacity(..) does not throw any more but produces a transparent window.

更多推荐

本文发布于:2023-08-06 12:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1448832.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:透明   JPanel   JLayeredPane   transparent   window

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!