iframe中的透明背景,但有窗口边框等

时间:2012-08-21 18:30:20

标签: java transparency awtutilities

所以我可以尝试问我的问题,但为了你和我的缘故,我在photoshop中创建了一个简单的图像,试图告诉你我想要实现的目标:

enter image description here

我曾尝试使用AWTUtilities中的Opaque东西,但它似乎不起作用你们有任何想法我怎么能实现这个?

我确实需要本机窗口边框,因此我可以拖放窗口,还需要调整大小功能。

提前致谢

1 个答案:

答案 0 :(得分:1)

对于透明/跨度JFrame,请参阅此处:http://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html,它解释了Java中的半透明Windows。

您需要致电setOpacity()

这也是一个小例子:

Screenshot for app

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() {
                TranslucentWindowDemo tw = new TranslucentWindowDemo();

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

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