简体   繁体   中英

Opaque components on transparent Java windows

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.

How can I fix this?

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);
}
}

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

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();
            }
        });
    }
}

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

Alpha set to 128 (half-transparent):

半透明标签

Alpha set to 0 (completely transparent):

完全透明的标签

Alpha set to 255 (completely opaque):

完全不透明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM