Java Swing:重绘()与无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4396583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Swing: repaint() vs invalidate
提问by Jason Rogers
Possible Duplicate:
Java Swing revalidate() vs repaint()
Hi all
大家好
I'm fighting with my program to make it refresh at the right time.
我正在努力让我的程序在正确的时间刷新。
And not having a lot of success lol
并没有取得很多成功,哈哈
I have 2 questions
我有2个问题
Q1: which should I use when my interface has changed: repaint or invalidate?
Q1:当我的界面发生变化时我应该使用哪个:重新绘制或失效?
Q2: when should they be called? I know it sounds stupid but I'm actually having problems because of SwingWorker and other threaded operations.
Q2:他们应该什么时候打电话?我知道这听起来很愚蠢,但由于 SwingWorker 和其他线程操作,我实际上遇到了问题。
采纳答案by Favonius
Q1: which should I use when my interface has changed: repaint or invalidate?
Q1:当我的界面发生变化时我应该使用哪个:重新绘制或失效?
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.
如果由于调整大小、字体更改等原因布局不是最新的,那么您应该调用 invalidate。使一个组件无效,使该组件无效,并且它上面的所有父组件都被标记为需要布局。在喷漆之前,在验证步骤中,如果没有发现任何变化,则喷漆步骤将被省略。
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()
).
如果组件的某个部分正在更新(由图形的剪辑矩形定义,称为“损坏”区域),那么您应该考虑调用重绘。可能出现损坏区域的原因之一是由于某些其他组件或应用程序而导致组件的一部分重叠。根据我的经验,如果在最内部的封闭组件上调用 repaint() 会更有效(即使用public void repaint(int x, int y, int width, int height)
而不是使用 public void repaint()
)。
Q2: when should they be called?
Q2:他们应该什么时候打电话?
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( ) 手动,因为几乎每次属性更改时,swing 都会为您执行此操作。
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.
当控件内有多个区域需要重绘时,Invalidate 会导致整个窗口一次重绘,避免重复重绘造成的闪烁。在实际重绘控件之前多次调用 Invalidate 没有性能损失。
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.
Repaint() :
如果组件是轻量级组件,则此方法会导致尽快调用此组件的绘制方法。否则,此方法会导致尽快调用此组件的更新方法。
Also have look at Update
method.
还有看Update
方法。
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.
注意:Swing 处理“重绘”请求的方式与 AWT 略有不同,尽管应用程序员的最终结果本质上是相同的——调用了paint()。
Refer to the link below for an excellent link on how painting is done in AWT and Swing:
有关如何在 AWT 和 Swing 中完成绘画的出色链接,请参阅以下链接:
http://www.oracle.com/technetwork/java/painting-140037.html
http://www.oracle.com/technetwork/java/painting-140037.html
Hope this will help.
希望这会有所帮助。