如何使 JLayeredPane() 和 JPanel() 透明?它总是显示超级窗口的背景

发布于 2024-11-25 01:42:17 字数 1874 浏览 0 评论 0原文

我怎样才能完全使这个灰色面板透明,以便我只能看到按钮“测试”,而看不到灰色框(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: enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚你发端 2024-12-02 01:42:17

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

编辑:在我的系统(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

编辑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:

  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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文