Java 使 JPanel 不绘制其背景(透明)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54926/
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
Make a JPanel not draw its background (Transparent)
提问by jjnguy
Is it possible, in Java, to make a JPanel
skip drawing its background thus being transparent except for the components on it?
在 Java 中,是否可以JPanel
跳过绘制其背景从而使其透明,除了其上的组件?
采纳答案by Allain Lalonde
setOpaque(false)
setOpaque(false)
It'll pass off painting the background to its parent, which may draw its own background.
它会将绘制背景传递给其父级,后者可能会绘制自己的背景。
You can do a screen capture and then use that to paint the background of the panel.
您可以进行屏幕截图,然后使用它来绘制面板的背景。
回答by Outlaw Programmer
This article seems to have some handy info on how to create shaped and transparent windows in Java:
这篇文章似乎有一些关于如何在 Java 中创建异形和透明窗口的方便信息:
https://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shaped_windows.html
https://docs.oracle.com/javase/tutorial/uiswing/misc/trans_shape_windows.html
回答by Tom Hawtin - tackline
Technically a JPanel may start off non-opague. This was true for the Gtk look & feel in 1.5 (or 1.4?), but no other PL&Fs as far as I am aware.
从技术上讲,JPanel 可以从非透明的开始。这对于 1.5(或 1.4?)中的 Gtk 外观和感觉是正确的,但据我所知没有其他 PL&F。
回答by user9349193413
class TransparentJPanel extends JPanel
{
TransparentJPanel()
{
super() ;
this.setOpaque( false ) ; // this will make the JPanel transparent
// but not its components (JLabel, TextField etc.)
this.setLayout( null ) ;
}
}
回答by Mustajeeb ur Rehman
If you are using NetBeans, Then Do these steps:-
如果您使用的是 NetBeans,请执行以下步骤:-
- Right Click on
JPanel
. - Scroll Down and Search For (( Opaque )). It must be there.
- Uncheck it.
- Now your
JPanel
background will be removed and what will appear inJPanel
Background is your Background ofJFrame
.
- 右键单击
JPanel
。 - 向下滚动并搜索 (( Opaque ))。它必须在那里。
- 取消选中它。
- 现在您的
JPanel
背景将被删除,JPanel
背景中将显示 您的背景JFrame
。