-1

我想显示 JLabel 但想隐藏 JFrame 边框和其他较低级别的容器,如 JPanel。它只是 JLabel 显示在屏幕上。

我尝试了窗口透明度,但如果尝试使用窗口不透明度,下面的代码会隐藏所有内容。在降低 windowOpacity 时,甚至 JLabel 也会变得模糊。我也尝试过使用 JPanel,但无法获得准确的输出。我只希望 jdk1.6 中的这种行为我希望 JLabel 内容正确可见而没有任何不透明度影响,但背景必须是纯透明的。

     public class TEST {

    public static void main(String[] args) {

        JFrame frame = new JFrame("Sanjaal Corps - Windows On Top Demo!");
        frame.setSize(400, 100);
        frame.setLocation(100, 150);        
        com.sun.awt.AWTUtilities.setWindowOpacity(frame,0.4f);
        frame.setUndecorated(true);
        frame.add(new JLabel("TESTING"));       
        frame.setAlwaysOnTop(true);
        frame.setVisible(true);

    }

}

我尝试使用提供的解决方案 http://www.dreamincode.net/forums/topic/140041-make-a-jpanel-transparent-to-see-the-desktop-behind/

但是这里的问题是如果我们最小化或最大化窗口,然后设置一个恒定的颜色,所以发现它不是最好的解决方案,或者说是完美的解决方案。

4

2 回答 2

3

假设您要显示作为其文本/图标的标签(没有别的)的前景,您可以将框架的不透明度设置为 false:

com.sun.awt.AWTUtilities.setWindowOpaque(frame, false);

反对使用 com.sun.** 类的常见警告,不幸的是,这是在 java7 之前到达透明窗口的唯一方法

于 2013-09-01T11:24:41.153 回答
3

假设我正确理解您的要求...

我通常会在Window. 这意味着,通常情况下,透明度属性Window不会影响子组件,例如......

在此处输入图像描述

一般来说,现在有两种方法可以使窗口透明。

  1. 在 Java 7 下,您只需将其背景颜色设置为透明即可。
  2. Java 6(更新10+)下,需要使用非官方的com.sun.AWTUtilities

...

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Window;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.Method;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class TransparentWindow02 {

    public static void main(String[] args) {
        new TransparentWindow02();
    }

    public TransparentWindow02() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Testing");
                frame.setUndecorated(true);
                setOpaque(frame, false);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setOpaque(false);
            setLayout(new BorderLayout());
            setBorder(new LineBorder(Color.RED));
            JLabel label = new JLabel("Click me if you can see me");
            label.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    SwingUtilities.windowForComponent(TestPane.this).dispose();
                }
            });
            add(label);
        }

    }

    public static void setOpaque(Window window, boolean opaque) {

        String version = System.getProperty("java.runtime.version");
        if (version.startsWith("1.7")) {
            window.setBackground(new Color(0, 0, 0, 0));
        } else {
            try {
                Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
                if (awtUtilsClass != null) {

                    Method method = awtUtilsClass.getMethod("setWindowOpaque", Window.class, boolean.class);
                    method.invoke(null, window, opaque);

                }
            } catch (Exception exp) {
                exp.printStackTrace();
            }
        }
    }
}
于 2013-09-01T11:32:04.063 回答