工具提示消失后如何重新绘制?
我创建了一个小程序,它有一个大面板来显示由多个控件(按钮、文本字段等)包围的数据。大面板包含我自己渲染的几层标签。
所有控件都有与之关联的工具提示,其中一些工具提示与主面板重叠。当它们消失时,它们会在主面板图像中留下一个洞,直到重新绘制主面板。
现在请注意,这种情况并不总是发生。仅当光标位于特定范围内时才会发生。如果向左或向右移动得足够远(沿 Y 轴的变化没有注意到差异),则当工具提示消失时,孔会被覆盖。
我不太熟悉工具提示和重绘应该如何工作,如果这表明我的程序有严重错误,但如果我可以在工具提示消失时在主面板上调用重绘,我应该没事。我可以在工具提示中重写某些内容来实现此目的吗?
我正在使用 Swing
谢谢。
I've created an applet which has one large panel to display data surrounded by several controls (buttons, textfields, etc.). The large panel contains several layers of labels which I render myself.
The controls all have tooltips associated with them, and some of these tooltips overlap the main panel. When they disappear, they leave a hole in the main panel image until the main panel is repainted.
Now mind you, this does not always happen. It only occurs when the cursor is in a certain range. If you get far enough to either the left or right (no difference noted for changes along the Y axis), the holes are painted over when the tooltip disappears.
I'm not well-versed on how tooltips and repainting are supposed to work, and if this is a sign that there's something dreadfully wrong with my program, but if I can just call repaint on the main panel whenever the tooltip disappears, I should be fine. Is there something I can override in tooltip to make this happen?
I'm using Swing
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答你的问题(在你通过评论找到解决方案之后): Swing 内置了一些相当复杂的重绘管理。当工具提示消失时,它下面的矩形将被重绘。
现在,哪些组件必须重新喷漆?所有与给定矩形重叠的组件,并且它们本身没有被其他组件隐藏(在相关区域中) - 但这里只计算不透明组件。 (这就是我们需要 JComponent 上的
opaque
属性的全部原因 - 以优化重新绘制。)您的标签声明自己是不透明的,但并没有真正在
paintComponent
,这样应该被标签覆盖的工具提示区域就保持未绘制状态。声明标签部分透明也会导致其后面组件的相关区域被重新绘制。
To answer your question (after you found a solution by the comments): Swing has some quite elaborate repaint management built in. When a tooltip disappears, the rectangle below it is repainted.
Now, which components have to be repainted? All those who overlap with the given rectangle, and are not themselves hidden (in the region in question) by other components - but only opaque components count here. (This is the whole reason we need the
opaque
property on JComponent - to optimize repainting.)Your label declared itself being opaque, but did not really paint its whole area on a
paintComponent
, and such the region of the tooltip which should have been covered by the label stayed unpainted.Declaring your label to be partly transparent caused also the concerning region of the component behind it to be repainted.