问题描述
我通过以下方式使JFrame透明化:
I am making my JFrame transparent by:
myFrame.setUndecorated(true);
myFrame.setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
这使JFrame透明,但也丢失了窗口条和关闭按钮.为了使窗口看起来可见,我使用了
It makes JFrame transparent but also lost window strip and close button.To make window appear visible I used
AWTUtilities.setOpacity(this, 0.5f);
但是我发现此方法仅适用于Java 6并打包在Java 7中受限的AWT程序包中,并且该方法也在Java 7(现在为
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
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:
这篇关于如何在Java中创建透明的JFrame背景,但保持按钮可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!