我有三个JPanel,fatherPanel,childPanel1,childrenPanel2。
当我单击按钮时,我会从父面板中删除当前的子面板,并在父面板中添加另一个子面板。
每次我都应该调用 revalidate() 和 repaint() 来更新 UI。
然后,我知道 SwingUtilities.updateComponentTreeUI() 具有相同的效果。
我想知道两者有什么区别吗?
最佳答案
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 有关布局更改的信息,但这是巨大的开销(理论上可能会产生一些副作用)。对于您的情况,结合使用 revalidate
和 repaint
效果更好。
关于java - 重绘和 SwingUtilities.updateComponentTreeUI 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37785121/