java 如何设置JDialog的透明背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4631021/
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
How to set transparent background of JDialog
提问by Christian 'fuzi' Orgler
Hy,..
嗨,..
how can i set the background transparent and "remove" the closeoperation (marked red) ? I only want to show the card :-)
如何将背景设置为透明并“删除”关闭操作(标记为红色)?我只想出示卡片:-)
Thanks..!
谢谢..!
回答by Tomek
Although there is no problem with UNDECORATED JFrame transparency (myJFrame.setBackground (new Color (0,0,0,0)); is pretty enough), the same with JDialog is not working.
尽管 UNDECORATED JFrame 透明度没有问题(myJFrame.setBackground (new Color (0,0,0,0)); 已经足够了),但 JDialog 的情况也没有问题。
I discovered, however, the following sequence works perfect for JDialog:
但是,我发现以下序列非常适合 JDialog:
myJDialog.getRootPane ().setOpaque (false);
myJDialog.getContentPane ().setBackground (new Color (0, 0, 0, 0));
myJDialog.setBackground (new Color (0, 0, 0, 0));
A also remain, but it is my PRIVATE, humble suggestion, that all setBackground call for Window extenders (e.g. JFrame, JDialog) should be tried against UnsupportedOperationException and IllegalComponentStateException.
一个仍然存在,但它是我的私人,谦虚的建议,应该针对 UnsupportedOperationException 和 IllegalComponentStateException 尝试对 Window 扩展程序(例如 JFrame、JDialog)的所有 setBackground 调用。
回答by LudoMC
yourDialog.setUndecorated(true)
should do the trick for the title bar.
yourDialog.setUndecorated(true)
应该做标题栏的伎俩。
For having the Frame transparent. You'll have to work on the root panel with yourDialog.getRootPane().setOpaque(false)
on it.
使框架透明。你必须在根面板yourDialog.getRootPane().setOpaque(false)
上工作。
回答by èIT
I followed the instructions from the article and it worked finnaly AND it wasn't difficult at all. :) I now have my translucent SplashImage ans About screen which displays a PNG image and respect its (complex) transparency. Just awesome. Note that the method to proceed will change a little bit in JDK 7.
我按照文章中的说明进行操作,它最终奏效了,而且一点也不难。:) 我现在有我的半透明 SplashImage ans About screen,它显示一个 PNG 图像并尊重它的(复杂的)透明度。太棒了。请注意,在 JDK 7 中继续的方法会有所改变。
Just notice the difference between keywords. http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html#6u10
请注意关键字之间的区别。 http://download.oracle.com/javase/tutorial/uiswing/misc/trans_shape_windows.html#6u10
It would have been nice if the
如果
yourDialog.setUndecorated(true);
yourDialog.getRootPane().setOpaque(false);
trick worked but it didn't to me. Maybe I did something wrong.
技巧奏效了,但对我来说却没有。也许我做错了什么。
I also note it is important to use setContentPane(Component); instead of getContentPane.add(Component);
我还注意到使用 setContentPane(Component) 很重要;而不是 getContentPane.add(Component);
I'm happy it works now ! :)
我很高兴它现在有效!:)