重绘和SwingUtilities.updateComponentTreeUI有什么区别?

时间:2016-06-13 08:35:27

标签: java swing

我有三个JPanel,fatherPanel,childPanel1,childrenPanel2。

当我点击一个按钮时,我从父面板中删除当前的子面板,并在父面板中添加另一个子面板。

每次我都应该调用revalidate()和repaint()来更新UI。

然后,我知道SwingUtilities.updateComponentTreeUI()具有相同的效果。

我想知道两人之间有什么区别吗?

1 个答案:

答案 0 :(得分:2)

Swing支持可插入的Look-n-Feel。在运行时更改L& F时,您需要使用方法updateComponentTreeUI通知所有组件有关此更改的信息。因为新的L& F组件大小可以更改,Swing必须调用revalidate来重新计算布局。以下是方法updateComponentTreeUI

的代码
/**
 * A simple minded look and feel change: ask each node in the tree
 * to <code>updateUI()</code> -- that is, to initialize its UI property
 * with the current look and feel.
 */
public static void updateComponentTreeUI(Component c) {
    updateComponentTreeUI0(c);
    c.invalidate();
    c.validate();
    c.repaint();
}

所以是的,您可以调用SwingUtilities.updateComponentTreeUI来通知您的GUI有关布局更改的信息,但这是一个巨大的开销(理论上可能有一些副作用)。在您的情况下,revalidaterepaint的组合更好。

相关问题