Java Swing - 半透明组件
我最近问了一个关于半透明组件因看似未正确更新而导致奇怪的工件的问题。我收到的答案导致伪像消失,但以半透明为代价。
解决方案是 - 对于每个半透明组件 - 也调用 setOpaque(false) 函数。这样,Swing 知道它需要重绘这些组件后面的背景。
然而,这是以我试图实现的半透明为代价的。它导致组件变得透明。
前提是这样的:我正在为一个聊天客户端设计GUI,一个功能要求是有一个背景。我通过遵循扩展 JPanel 类的代码片段成功地使背景正常工作,但随后我希望组件允许显示背景。设置半透明度后,更新组件的剩余部分会显示在不应该显示的位置。我来到这里解决了我的问题,但现在我遇到了一个新问题。所以我们到了。
所以,这就是我的猜测:
- 为每个所需的组件调用 setOpaque(false) 函数而不设置半透明颜色并不能达到我想要的效果。
- 设置半透明颜色而不调用 setOpaque(false) 允许显示半透明背景,但会导致伪像,使我回到第一个方向。
因此,我需要在没有伪影的透明和有伪影的半透明之间找到一些中间立场。也就是说,我想要一个没有伪影的半透明背景(不是完全透明)。
看来我需要重写 JFrame 以使其重新绘制所有组件,无论不透明度如何。除非我错过了什么……这就是我来这里的原因!
谢谢!
(这里是原始问题的链接,并附有图片供参考:Java Swing - 半透明组件造成伪影)
I recently asked a question about translucent components causing odd artifacts from seemingly not updating properly. The answer I received caused the artifacts to go away, but at the cost of translucency.
The solution was to- for every translucent component- also call the setOpaque(false) function. This way, Swing knew that it needed to redraw the background behind those components.
However, this came at the cost of the translucency that I was trying to achieve. It caused the components to become transparent instead.
The premise is this: I am designing the GUI for a chat client, and a feature request was to have a background. I successfully got the background working by following a code snippet for extending the JPanel class, but then I wanted the components to allow the background to show. After setting their translucency, remnants of updated components were being displayed where they shouldn't have been. I came here and got my problem solved, but now I've got a new problem. So here we are.
So, here is what I've surmised:
-Calling the setOpaque(false) function for each desired component and NOT setting a translucent color does not achieve what I want.
-Setting a translucent color and NOT calling setOpaque(false) allows the translucent background to show, but causes artifacts, putting me back at square one.
So I need some middle ground between transparent with no artifacts, and translucent with artifacts. Namely, I want a translucent background (not completely transparent) that has no artifacts.
It seems like I'm going to need to override the JFrame to cause it to repaint all its components, regardless of the opacity. Unless there's something I'm missing.. which is why I'm here!
Thanks!
(Here's a link to the original question, with a picture for reference: Java Swing - Translucent Components causing Artifacts)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种选择是覆盖组件并自己绘制背景:
编辑:或者,您可以将子组件的半透明背景颜色直接绘制到面板上,然后您不必覆盖组件:
这种方法有一个缺点。如果组件有真正透明的部分(例如圆形边框),则其整个背景将被着色。
One option would be to override the components and draw the background yourself:
EDIT: Alternatively you could draw the translucent background colour for the child components directly onto the panel, then you would not have to override components:
There is a disadvantage to this approach. If there is a genuinely transparent part of a component (such as a rounded border), then its entire background will be coloured.