透明Java窗口上的不透明组件

编程入门 行业动态 更新时间:2024-10-09 17:23:40
本文介绍了透明Java窗口上的不透明组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经成功地使Java窗口透明,但是在将不透明组件叠加到这些窗口上时遇到了麻烦.JFrame.setOpacity(0)和AWTUtilities setWindowOpacity都将透明性传递给组成组件.此外,JFrame.setBackground(0,0,0,0)以某种方式使这些组件失去了透明度.

I've been successful in making java windows transparent, but I'm having trouble superimposing opaque components on top of those windows. JFrame.setOpacity(0) and AWTUtilities setWindowOpacity all transfer transparency to constituent components. In addition, JFrame.setBackground(0,0,0,0) somehow bleeds transparency to said components.

我该如何解决?

测试类:分别是透明背景,setOpacity和AWTUtility

test classes: transparent background, setOpacity, and AWTUtility, respectively

import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.Color; public class test { public static void main(String[] args){ JFrame frame = new JFrame("test"); JLabel label = new JLabel("Label text"); frame.setUndecorated(true); frame.setBackground(new Color(0,0,0,128)); frame.add(label); frame.pack(); frame.setVisible(true); } } public class test2 { public static void main(String[] args){ JFrame frame = new JFrame("test"); JLabel label = new JLabel("Label text"); frame.setUndecorated(true); frame.setOpacity(.50f); frame.add(label); frame.pack(); frame.setVisible(true); } } import com.sun.awt.AWTUtilities; import java.lang.reflect.Method; import java.awt.Window; public class test3 { public static void main(String[] args){ JFrame frame = new JFrame("test"); JLabel label = new JLabel("Label text"); frame.setUndecorated(true); try { Class<?> awtUtilitiesClass = Class.forName("com.sun.awt.AWTUtilities"); Method mSetWindowOpacity = awtUtilitiesClass.getMethod("setWindowOpacity", Window.class, float.class); mSetWindowOpacity.invoke(null, frame, Float.valueOf(0.50f)); } catch (Exception x){} frame.add(label); frame.pack(); frame.setVisible(true); } }

我已经在Windows上尝试过setBackground(0,0,0,0)了,但在Linux(xfce)上却无法正常工作.

I've tried setBackground(0,0,0,0) on Windows, where it works, but it doesn't work properly on Linux (xfce).

推荐答案

使用AWTUtilties.setOpaque(Window,boolean),您可以得到想要的东西.这是一个半透明标签(带有红色背景)的示例:

Using AWTUtilties.setOpaque(Window, boolean), you can get what you want. Here is an example of a half-transparent label (with a red background):

import java.awt.Color; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; import com.sun.awt.AWTUtilities; public class Test3 { protected static void initUI() { JFrame frame = new JFrame("test"); JLabel label = new JLabel("Label text"); label.setOpaque(true); label.setBackground(new Color(255, 0, 0, 128)); frame.setUndecorated(true); AWTUtilities.setWindowOpaque(frame, false); frame.add(label); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { initUI(); } }); } }

以下是一些截屏,其中的alpha chanel(在白色背景上制作)具有不同的值:

Here are some screenshots with different values for the alpha chanel (made on a white background):

Alpha设置为128(半透明):

Alpha set to 128 (half-transparent):

Alpha设置为0(完全透明):

Alpha set to 0 (completely transparent):

Alpha设置为255(完全不透明):

Alpha set to 255 (completely opaque):

更多推荐

透明Java窗口上的不透明组件

本文发布于:2023-11-05 02:49:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559702.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组件   不透明   透明   窗口   Java

发布评论

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

>www.elefans.com

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