Java Swing:repaint()vs invalidate [英] Java Swing: repaint() vs invalidate

查看:104
本文介绍了Java Swing:repaint()vs invalidate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

Java Swing revalidate()vs repaint()

嗨所有

我正在与我的计划斗争,以便在合适的时间刷新。

I'm fighting with my program to make it refresh at the right time.

没有很多成功lol

我有2个问题

Q1:当我的界面发生变化时我应该使用哪个:重绘还是无效?

Q1: which should I use when my interface has changed: repaint or invalidate?

Q2:什么时候应该被叫?我知道这听起来很愚蠢,但由于SwingWorker和其他线程操作,我实际上遇到了问题。

Q2: when should they be called? I know it sounds stupid but I'm actually having problems because of SwingWorker and other threaded operations.

推荐答案


Q1:当我的
界面发生变化时我应该使用哪个:重绘或
无效?

Q1: which should I use when my interface has changed: repaint or invalidate?

如果由于调整大小,字体更改等原因,布局不是最新的,因此您应该调用invalidate。使组件无效,使组件无效,并将其上方的所有父项标记为需要布局。在绘制之前,在验证步骤中如果没有找到任何更改,则省略绘制步骤。

If the layout is not up to date because of resizing , font change etc then you should call invalidate. Invalidating a component, invalidates the component and all parents above it are marked as needing to be laid out. Prior to painting, in the validation step if no change is found then the paint step is left out.

如果有一部分组件正在更新(由图形的剪辑矩形,称为损坏区域),那么你应该考虑调用重绘。可能发生损坏区域的原因之一是由于某些其他组件或应用程序而导致组件的一部分重叠。
根据我的经验,如果你在最里面的封闭组件上调用它,那么repaint()会更有效(即使用 public void repaint(int x,int y,int width,int height)而不是使用公共 void repaint())。

If there is some part of component which is being updated (defined by the graphic's clip rectangle, called "damaged" region) then you should consider calling repaint. One of the reason a damaged regions may occur is from the overlapping of a part of your component because of some other component or application. As per my experience the repaint() is more effective if you call it on the innermost enclosing component (i.e. using public void repaint(int x, int y, int width, int height) rather than using public void repaint()).


Q2:什么时候应该被叫?

Q2: when should they be called?

Invalidate():标记一个组件无效 - 这意味着,它的布局是或者可能不是最新的:即组件调整大小,添加边框,更改字体等等,你永远不需要调用invalidate()手动,因为swing会对你进行每次属性更改。

Invalidate(): marks a component as not valid -- that means, it's layout is or may not be "up to date" anymore: i.e. the component is resized, a border is added, it's font changes, etc. you should never need to call invalidate() by hand, as swing does that for you on pretty much for every property change.

当控件中的多个区域需要重新绘制时,Invalidate将导致整个窗口一次性重涂,避免冗余重涂造成的闪烁。在实际重新绘制控件之前多次调用Invalidate没有性能损失。

When more than one region within the control needs repainting, Invalidate will cause the entire window to be repainted in a single pass, avoiding flicker caused by redundant repaints. There is no performance penalty for calling Invalidate multiple times before the control is actually repainted.

Repaint():如果组件是轻量级组件,则此方法会调用此组件的paint方法尽快地。否则,此方法会尽快调用此组件的更新方法。

Repaint() : If the component is a lightweight component, this method causes a call to this component's paint method as soon as possible. Otherwise, this method causes a call to this component's update method as soon as possible.

还要看更新方法。

注意: Swing进程以与AWT略有不同的方式重绘请求,尽管应用程序员的最终结果基本相同 - 调用了paint()。

NOTE: Swing processes "repaint" requests in a slightly different way from the AWT, although the final result for the application programmer is essentially the same -- paint() is invoked.

请参阅以下链接,了解如何在AWT和Swing中完成绘画:

Refer to the link below for an excellent link on how painting is done in AWT and Swing:

< a href =http://www.oracle.com/technetwork/java/painting-140037.html\"rel =noreferrer> http://www.oracle.com/technetwork/java/painting-140037.html

希望这会有所帮助。

这篇关于Java Swing:repaint()vs invalidate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆