2016-06-13 13 views
0

我有三個JPanel,fatherPanel,childPanel1,childrenPanel2。repaint和SwingUtilities.updateComponentTreeUI有什麼區別?

當我點擊一個按鈕時,我從父面板中刪除當前的子面板,並在父面板中添加另一個子面板。

每次我應該調用revalidate()和repaint()來更新UI。

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

我想知道兩者之間有什麼區別嗎?

回答

2

Swing支持可插拔Look-n-Feel's。當您在運行時更改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的組合更好。