0

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

我需要做什么才能使其工作?

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

1 回答 1

0

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

我尝试在 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();
    }
}
于 2013-04-05T23:24:26.163 回答