我已经成功地让java窗口透明,但是我在将不透明的组件添加到这些窗口上方时遇到了麻烦。 JFrame.setOpacity(0)和AWTUtilities setWindowOpacity都将透明度转换为构成组件。另外,JFrame.setBackground(0,0,0,0)以某种方式向所述组件泄露透明度。透明Java窗口上的不透明组件
我该如何解决这个问题?
测试类:透明背景,setOpacity和AWTUtility,分别
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);
}
}
编辑:我试过的setBackground(0,0,0,0)在Windows上,它的工作原理,但它不” t在Linux(xfce)上正常工作。
为了更好地帮助越早,张贴[SSCCE](http://sscce.org/)。 –