java中的重绘

void java.awt.Container.validate()
Validates this container and all of its subcomponents.
这个函数更新容器及其全部子控件,更新容器意味着重新布置它的子控件。布局相关的更改(例如设置子控件的大小位置,向容器添加新控件)会invalidate这个容器。需要注意,当前容器invalidate会自动导致它的父容器进行invalidate(详见Component.invalidate)。因此,要想恢复整个层次的有效性(validity),validate()这个函数应该由最高级别的父容器来调用。容器的validate()是一个费时的操作,故为了性能,程序员最好不要频繁调用它。比如,应该在对容器全部更改完成之后,只调用一次validate(),而不应该是每改动一下都validate一次。如果当前容器不有效,那就会调用它的invalidateTree()函数进行validate它的全部子控件。如果当前容器有效,那就啥都不做。
Validating a container means laying out its subcomponents. Layout-related changes, such as setting the bounds of a component, or adding a component to the container, invalidate the container automatically. Note that the ancestors of the container may be invalidated also (see Component.invalidate for details.) Therefore, to restore the validity of the hierarchy, the validate() method should be invoked on the top-most invalid container of the hierarchy.

Validating the container may be a quite time-consuming operation. For performance reasons a developer may postpone the validation of the hierarchy till a set of layout-related operations completes, e.g. after adding all the children to the container.

If this Container is not valid, this method invokes the validateTree method and marks this Container as valid. Otherwise, no action is performed.

==============
void java.awt.Container.invalidate()

这个函数使得容器失效,例如:容器的布局为layoutManager2,这个函数会调用layoutManager2.invalidateLayout(Container)。之后,这个函数把这个容器标记为“失效(invalid)”,并且把这个容器的各级父容器都标记为“失效(invalid)”。
Invalidates the container.

If the LayoutManager installed on this container is an instance of the LayoutManager2 interface, then the LayoutManager2.invalidateLayout(Container) method is invoked on it supplying this Container as the argument.

Afterwards this method marks this container invalid, and invalidates its ancestors. See the Component.invalidate method for more details.

===========================
void javax.swing.JComponent.update(Graphics g)

这个函数调用paint(),它不擦除背景。
Calls paint. Doesn't clear the background but see ComponentUI.update, which is called by paintComponent.
==========================
void javax.swing.JPanel.updateUI()

当观感改变之后,调用这个函数进行更新。
Resets the UI property with a value from the current look and feel.
=======================
void java.awt.Component.repaint()

重绘控件,如果是轻量级控件,这个函数立即调用它的paint()函数。否则,这个函数会立即调用它的update()函数
Repaints this component.

If this 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.

Note: For more information on the paint mechanisms utilitized by AWT and Swing, including information on how to write the most efficient painting code, see Painting in AWT and Swing.

======================

涉及到重绘的,update()+repaint()足以应付一切情况。update()和invalidate()之后,不会立即引起重绘事件,用repaint()则能够立即重绘。

转载于:https://www.cnblogs.com/weiyinfu/p/5131985.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值