透明Java窗口上的不透明组件 [英] Opaque components on transparent Java windows

查看:45
本文介绍了透明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窗口上的不透明组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆