JPanel透明度问题
我有一个深灰色的 JPanel
,上面有一个 JLabel
。我将 new Color(0, 0, 0, .5f)
(透明)设置为 JLabel
的背景,并使用按钮多次更改文本。问题是,每次更改文本时,之前的文本仍然保留在新文本后面。我将文本从“123456789”更改为“1234567”、“12345”和“123”。这是屏幕截图:
如何摆脱这个“阴影”?
I have a dark-gray JPanel
with a JLabel
on it. I set new Color(0, 0, 0, .5f)
(tranparent) as the background of the JLabel
and I change the text several times using a button. The problem is, everytime the text is changed, the previous text still remains behind the new text. I change the text from "123456789" to "1234567", "12345" and "123". Here is the screenshot:
How do I get rid of this "shadow"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Swing 不支持透明背景。
Swing 期望组件是:
setOpaque(...)
方法用于控制组件的不透明属性。在任何一种情况下,这都可以确保消除任何绘画伪影,并且可以正确完成自定义绘画。
如果您想使用透明度,那么您需要自己进行自定义绘画以确保背景被清除。
面板的自定义绘制如下:
每个使用透明度的组件都需要类似的代码。
或者,您可以查看具有透明度的背景进行自定义可以在任何为您完成上述工作的组件上使用的类。
Swing does not support transparent backgrounds.
Swing expects a component to be either:
The
setOpaque(...)
method is used to control the opaque property of a component.In either case this makes sure any painting artifacts are removed and custom painting can be done properly.
If you want to use tranparency, then you need to do custom painting yourself to make sure the background is cleared.
The custom painting for the panel would be:
Similar code would be required for every component that uses transparency.
Or, you can check out Background With Transparency for custom class that can be used on any component that will do the above work for you.
此相关示例也使
JPanel
半透明。This related example also makes the
JPanel
translucent.试试这个,也许它能解决你的问题:
行动中表现..
try this, maybe it will solve your problem:
In actionPeroformed..