繁体   English   中英

Java Swing中的透明JFrame

[英]Transparent JFrame in java swing

我需要在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(); 
    }
}

根据您的平台,“窗口透明度”可能仅在默认(金属)外观下起作用。

我尝试在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();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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