java swing中的透明JFrame

问题描述 投票:0回答:1

我需要在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(); 
    }
}
java swing transparency paint translucency
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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.