JTabbedPane摆动更新错误
我在 JTabbedPane 中有 2 个 JPanel,当在第一个面板内的面板上调用 update(g) 时(它是一个动画),即使第二个面板是选定的面板(即您可以看到的面板),更新的面板也会显示在屏幕。这是为什么?我怎样才能避免这种行为?
I have 2 JPanels in a JTabbedPane and when update(g) is called on a panel inside the first panel (Its an animation) even if the second panel is the selected panel(i.e the one you can see) the updated panel appears on the screen. Why is this and how can i circumvent this behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
<
JComponent
的 code>update() 方法“不会清除背景”,因此您可能需要明确执行此操作。JTabbedPane
通常不需要使用 < a href="http://java.sun.com/javase/6/docs/api/javax/swing/JComponent.html#update%28java.awt.Graphics%29" rel="nofollow noreferrer">更新()
。也许 sscce 显示您的使用情况可能会有所帮助。附录 1:不清楚您为何调用
update()
。下面是一个没有表现出异常的简单动画。附录 2:请参阅在 AWT 和 Swing 中绘制:paint () 与 update()。您可能想在
actionPerformed()
中使用repaint()
。The
update()
method ofJComponent
"doesn't clear the background," so you may need to do that explicitly. Typical examples ofJTabbedPane
don't usually require usingupdate()
. Perhaps an sscce showing your usage might help.Addendum 1: It's not clear why you are calling
update()
. Below is a simple animation that does not exhibit the anomaly.Addendum 2: See Painting in AWT and Swing: paint() vs. update(). You may want to use
repaint()
inactionPerformed()
instead.重写 update(...) 方法是一个古老的 AWT 技巧,永远不应该与 Swing 一起使用。
阅读 Swing 教程中关于自定义绘制的部分执行此操作的正确方法。对于动画,请阅读教程中的“如何使用 Swing Timer”。
Overriding the update(...) method is an old AWT trick and should never be used with Swing.
Read the section from the Swing tutorial on Custom Painting for the proper way to do this. And for animation read up on "How to Use Swing Timer" found in the tutorial as well.