【问题标题】:Transparent JFrame in java swingjava swing中的透明JFrame
【发布时间】:2013-04-05 20:02:03
【问题描述】:

我需要在 java swing 中创建一个透明的 jframe,它的不透明度为 0.05f。我尝试了下面的代码,但它不起作用。我在 Windows 中工作。

我需要做什么才能让它工作?

import java.awt.AlphaComposite;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import java.awt.Color;


public class BackgroundNull {

   private JFrame frame;

    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                BackgroundNull window = new BackgroundNull();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public BackgroundNull() {
    initialize();

private void initialize() {
    frame = new JFrame();
    frame.setBackground(new Color(0, 0, 0, 0));
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setOpacity(0.5f);
}

    public void paint(Graphics g) { 
        Graphics2D g2 = (Graphics2D) g.create(); 
        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.0f)); 

        frame.getContentPane().paint(g2); 
        g2.dispose(); 
    }
}

【问题讨论】:

标签: java swing transparency paint translucency


【解决方案1】:

根据您的平台,窗口透明度可能仅适用于默认(金属)外观。

我尝试在 Mac OSX 和 Windows 7 上使用系统外观和感觉,两者都可以工作。

您的代码中缺少的部分是...

JFrame.setDefaultLookAndFeelDecorated(true);

import java.awt.*;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;

public class TranslucentWindowDemo extends JFrame {

    public TranslucentWindowDemo() {
        super("TranslucentWindow");
        setLayout(new GridBagLayout());

        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add a sample button.
        add(new JButton("I am a Button"));
    }

    public static void main(String[] args) {
        // Determine if the GraphicsDevice supports translucency.
        GraphicsEnvironment ge =
                        GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();

        //If translucent windows aren't supported, exit.
        if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
            System.err.println(
                            "Translucency is not supported");
            System.exit(0);
        }

        JFrame.setDefaultLookAndFeelDecorated(true);

        // Create the GUI on the event-dispatching thread
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }
                TranslucentWindowDemo tw = new TranslucentWindowDemo();

                // Set the window to 55% opaque (45% translucent).
                tw.setOpacity(0.55f);

                // Display the window.
                tw.setVisible(true);
            }
        });
    }
}

现在,有一个不幸的消息。在 Java 6 下,你可以让它工作。

以下代码(在 Java 6 下)将使原生 Window 透明...

public static void setOpacity(Window window, float opacity) {
    try {
        Class<?> awtUtilsClass = Class.forName("com.sun.awt.AWTUtilities");
        if (awtUtilsClass != null) {
            Method method = awtUtilsClass.getMethod("setWindowOpacity", Window.class, float.class);
            method.invoke(null, window, opacity);
        }
    } catch (Exception exp) {
        exp.printStackTrace();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-31
    • 2011-06-26
    • 1970-01-01
    • 1970-01-01
    • 2014-05-28
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多