java:如何将透明渐变背景添加到 JFrame

2022-01-24 00:00:00 gradient java swing jframe

java:我想对 JFrame 使用渐变风格的透明背景.顶部的透明度应为 100%,但向下时应继续降低,底部应为 20%

我知道我可以使用已经具有这种效果的图像,但我想提供主题工具并允许用户使用他们喜欢的图像,但在运行时允许透明.

解决方案

Sun 在 6u10 中为 java 添加了对半透明背景的支持,但 API 中没有正式描述.在 Java 7 中,该功能通过 Window 类.

Oracle 此处

在底部有一个渐变效果的代码示例.

该技术只有在底层操作系统窗口管理器支持时才有效.X11 (Linux) 需要正确安装和配置合成窗口管理器.这记录在 已知问题Java 7 发行说明和此错误.

java: I want to use the Gradient style Transparent Background to JFrame. On the top the transparency should be 100% but when going down it should go on decreasing and at the bottom it should be 20%

I know i can use the images already having such effect but i want to provide the themes facility and allowing user to use their favorite images but allow transparency at the run time.

解决方案

Sun added support for translucent backgrounds to java in 6u10 but it is not formally described in the API. In Java 7 the functionality was formally added to the API via the setBackground(), setOpacity(), and setShape() methods of the Window class.

The behavior is described by Oracle here

Towards the bottom there is a code example for the gradient effect.

The technique will only work if the underlying OS window manager supports it. X11 (Linux) requires a compositing window manager to be installed and configured correctly. This is documented in the known issues of the Java 7 release notes and in this bug.

相关文章