摆动刷新周期 [英] Swing refresh cycle

查看:43
本文介绍了摆动刷新周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解何时使用重新验证/重新绘制/打包.

I'm trying to understand when to use revalidate/repaint/pack.

令人惊讶的是,我没有找到太多详细的底层文档(请随意链接).

Surprisingly I haven't found much detailed under-the-hood documentation (feel free to link).

到目前为止,我已经明白这是 RepaintManager 的全部责任.

So far I have understood that this is all the responsibility of the RepaintManager.

  • 油漆/重新油漆指的是脏/干净的东西
  • pack/validate/revalidate 指的是什么是有效的

这篇关于 Oracle 的文章 解释了调用重绘入队Event Dispatcher Thread 上的一项工作,它将依次调用组件上的paintImmediately()(这会重绘组件).

This article on Oracle explains that calling a repaint enqueues a job on the Event Dispatcher Thread that will in turn call paintImmediately() on the component (this redraws the component).

这条线索表明是或不是valid 与 LayoutManager 相关联.而且这完全是关于组件的大小而不是内容.

This trail indicates that to be or not to be valid is associated with the LayoutManager. And that this is all about the size of the component rather than the content.

  1. 是否应该在移动/调整组件大小时调用 revalidate 并在更改其内容时重新绘制?
  2. pack() 方法真的是一个你永远不应该调用的弃用方法吗?
  3. 上述说法是否有误?

推荐答案

以下是一些您需要调用这些方法的基本情况(我涵盖了基础知识,但我可能错过了其他一些调用这些方法的情况)必填).

Here are a few basic cases where you need to invoke those methods (I cover the basics but I may have missed a few other cases where calling those methods would be required).

  1. 当您具有以下任一情况时,您应该在容器上调用 revalidate():添加一个或多个组件、删除一个或多个组件、更改一个或多个包含的组件的约束(约束或 XXXSize(),虽然不推荐后者),但是改变了容器的LayoutManager.
  2. 只要您希望重新绘制该组件(及其后代),就应该调用 repaint().最终,这将调用 paintComponent()(对于基本小部件,这将委托给 XXXUI.paint())、paintBorder()paintChildren()(至少在 Swing 中)
  3. pack() 实际上将窗口的大小设置为其首选大小.您通常应该在使窗口可见之前调用它.您可以稍后调用它,但这会带来奇怪的用户体验(我不知道许多应用程序会在显示后调整其窗口大小)
  1. You should call revalidate() on a container when you have either: added one or more component, removed one or more components, changed the constraints of one or more contained components (constraints or XXXSize(), although the latter is not recommended), changed the LayoutManager of the container.
  2. You should call repaint() whenever you want that component (and its descendants) to be repainted. Eventually, this will call paintComponent() (for basic widgets this will delegate to XXXUI.paint()), paintBorder() and paintChildren() (at least in Swing)
  3. pack() actually sets the size of a window to its preferred size. You should usually call this right before making the window visible. You may call it later on but this will give a weird user experience (I don't know many applications that resize their windows once displayed)

使用 revalidate()repaint() 的主要优点是它们可以自己合并.这意味着如果多次调用repaint(),则绘制只会执行一次.

The main advantage of using revalidate() and repaint() is that they can coalesce themselves. This means that if you call several times repaint(), the painting will only be performed once.

这篇关于摆动刷新周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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