Module java.desktop
Package javax.swing.plaf

Interface UIResource

所有已知的实现类:
ActionMapUIResource, BasicBorders.ButtonBorder, BasicBorders.FieldBorder, BasicBorders.MarginBorder, BasicBorders.MenuBarBorder, BasicBorders.RadioButtonBorder, BasicBorders.RolloverButtonBorder, BasicBorders.SplitPaneBorder, BasicBorders.ToggleButtonBorder, BasicComboBoxEditor.UIResource, BasicComboBoxRenderer.UIResource, BasicTextUI.BasicCaret, BasicTextUI.BasicHighlighter, BorderUIResource, BorderUIResource.BevelBorderUIResource, BorderUIResource.CompoundBorderUIResource, BorderUIResource.EmptyBorderUIResource, BorderUIResource.EtchedBorderUIResource, BorderUIResource.LineBorderUIResource, BorderUIResource.MatteBorderUIResource, BorderUIResource.TitledBorderUIResource, ColorUIResource, ComponentInputMapUIResource, DefaultListCellRenderer.UIResource, DefaultMenuLayout, DefaultTableCellRenderer.UIResource, DimensionUIResource, FontUIResource, IconUIResource, InputMapUIResource, InsetsUIResource, JScrollPane.ScrollBar, MetalBorders.ButtonBorder, MetalBorders.Flush3DBorder, MetalBorders.InternalFrameBorder, MetalBorders.MenuBarBorder, MetalBorders.MenuItemBorder, MetalBorders.OptionDialogBorder, MetalBorders.PaletteBorder, MetalBorders.PopupMenuBorder, MetalBorders.RolloverButtonBorder, MetalBorders.ScrollPaneBorder, MetalBorders.TextFieldBorder, MetalBorders.ToggleButtonBorder, MetalBorders.ToolBarBorder, MetalCheckBoxIcon, MetalComboBoxEditor.UIResource, MetalIconFactory.PaletteCloseIcon, ScrollPaneLayout.UIResource

public interface UIResource
此接口用于标记由ComponentUI代理创建的对象。ComponentUI.installUI()和ComponentUI.uninstallUI()方法可以使用此接口来判断属性值是否已被覆盖。例如,JList的cellRenderer属性由BasicListUI.installUI()初始化,仅当其初始值为null时:
 if (list.getCellRenderer() == null) {
     list.setCellRenderer((ListCellRenderer)(UIManager.get("List.cellRenderer")));
 }
 
在uninstallUI()时,如果属性值是UIResource的实例,我们将将属性重置为null:
 if (list.getCellRenderer() instanceof UIResource) {
     list.setCellRenderer(null);
 }
此模式适用于除java.awt.Component属性font、foreground和background之外的所有属性。如果这些属性之一未初始化,或者明确设置为null,则其容器提供值。因此,在installUI()调用以动态更改组件的外观和感觉时,使用"== null"是不可靠的。因此,在installUI()时,我们检查当前值是否为UIResource:
 if (!(list.getFont() instanceof UIResource)) {
     list.setFont(UIManager.getFont("List.font"));
 }
 
参见: