首页 > 解决方案 > 如何在 Linux 上支持 JFrame 透明性

问题描述

我正在制作一个 java swing 应用程序,它在 linux 机器上执行并通过 X11 转发显示在用户的计算机上。

我想在 JFrame 上应用圆角,所以我将其背景更改为新颜色(0,0,0,0)。
该框架现在隐藏在我的计算机上(Windows 10)。但是 linux(centOS7) 似乎不支持窗口框架透明度。

它在下面返回错误。

    Exception in thread "AWT-EventQueue-0" 
    java.lang.UnsupportedOperationException: PERPIXEL_TRANSLUCENT translucency is not supported
    at java.awt.Window.setBackground(Window.java:3844)
    at java.awt.Frame.setBackground(Frame.java:988)

在不改变其背景的情况下,两者都可以正常工作,但也会显示默认背景。
有什么办法可以做圆角吗??
谢谢你。

标签: swingrounded-corners

解决方案


并非每个平台都支持透明度。

看看https://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shape_windows.html

确定平台的能力

import static java.awt.GraphicsDevice.WindowTranslucency.*;

// Determine what the default GraphicsDevice can support.
GraphicsEnvironment ge =
    GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();

boolean isUniformTranslucencySupported =
    gd.isWindowTranslucencySupported(TRANSLUCENT);
boolean isPerPixelTranslucencySupported =
    gd.isWindowTranslucencySupported(PERPIXEL_TRANSLUCENT);
boolean isShapedWindowSupported =
    gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT);

有待测试,但我不认为 X11 转发允许透明!

编辑 刚刚通过 ssh -X,java 版本 1.8.0_162-b12 做了一个测试,它可以工作。你的java版本是什么?

在此处输入图像描述


推荐阅读