Java 透明 JFrame 背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2533650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Transparent JFrame background
提问by user
Is it possible to make a JFrame that has a transparent background and draw an image on it, so only the image will be visible with no border or background?
是否可以制作一个具有透明背景的 JFrame 并在其上绘制图像,以便只有图像可见而没有边框或背景?
采纳答案by McDowell
See Translucent and Shaped Swing Windowsby Kirill Grouchnikov.
参见Kirill Grouchnikov 的半透明和异形摇摆窗。
回答by trashgod
For a Mac OS X example, see Re-paint problem on translucent frame/panel/component.
对于 Mac OS X 示例,请参阅半透明框架/面板/组件上的重新绘制问题。
回答by Taylor Golden
It is possible.
有可能的。
If your JFrame is a local variable or field:
如果您的 JFrame 是局部变量或字段:
myJFrame.setUndecorated(true);
If your class extends JFrame:
如果您的类扩展 JFrame:
setUndecorated(true);
回答by Punyapat
Yes, it's possible in many ways. This is one of them:
是的,这在很多方面都是可能的。这是其中之一:
setUndecorated(true);
setBackground(new Color(1.0f,1.0f,1.0f,0.5f));
4th float (which I set to 0.5f) in Color's constructor is alpha channel. It can be 0.0f - 1.0f depend on transparency you want.
Color 的构造函数中的第 4 个浮点数(我设置为 0.5f)是 alpha 通道。它可以是 0.0f - 1.0f,具体取决于您想要的透明度。
回答by ossobuko
You should make content pane transparent too.
您也应该使内容窗格透明。
frame.setUndecorated(true);
frame.getContentPane().setBackground(new Color(1.0f,1.0f,1.0f,0.0f));
frame.setBackground(new Color(1.0f,1.0f,1.0f,0.0f));