简体   繁体   English

如何在java中创建透明的JFrame背景但保持按钮可见?

[英]How to create background of JFrame transparent in java but keeping buttons visible?

I am making my JFrame transparent by:我通过以下方式使我的 JFrame 透明:

myFrame.setUndecorated(true);
myFrame.setBackground(new Color(1.0f,1.0f,1.0f,0.0f)); 

It makes JFrame transparent but also lost window strip and close button.它使 JFrame 透明,但也丢失了窗口条和关闭按钮。 To make window appear visible I used为了使窗口可见,我用过

AWTUtilities.setOpacity(this, 0.5f);

but I found that this method is only available for java 6 and packaged in AWT package which is restricted in java 7. and the method is also changed in java 7 which is now但是我发现这个方法只适用于java 6并且打包在java 7中限制的AWT包中。并且该方法在java 7中也发生了变化,现在是

Window.setOpacity(float opacity);

but it does not working, Can anyone tell me how to make transparent window and buttons visible along with transparent frame.但它不起作用,谁能告诉我如何使透明窗口和按钮与透明框架一起可见。

This will make the frame look a bit different, but I think this is what you want:这将使框架看起来有点不同,但我认为这是你想要的:

import java.awt.Color;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class TransparentExample extends JFrame {

    public TransparentExample() {

        super("TransparentExample");

        JPanel panel = new JPanel();
        panel.add(new JButton("Button"));

        setContentPane(panel);
        setBackground(new Color(0, 0, 0, 0));
        setSize(500, 500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {

        JFrame.setDefaultLookAndFeelDecorated(true);

        TransparentExample frame = new TransparentExample();
        frame.setVisible(true);

    }

}

If it works it should look like this:如果它有效,它应该是这样的:

在此处输入图片说明

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

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