这几天打算写一个swing的程序,对于swing的look and feel比较感兴趣,进行了一小部分的研究。首先对于设置look and feel开发,首先查看设置程序。
try {
UIManager.setLookAndFeel(new CustomLookAndFeel());
} catch (UnsupportedLookAndFeelException e) {
throw new RuntimeException(e);
}
基本的代码就是如此,研究一下代码
1。CustomLookAndFeel,主要是编写的Look and feel的类,其中在jdk中的javax.swing.plaf中定义了一些抽象类,javax.swing.plaf.basic中定义了一些基本的扩展。如果自定义laf,建议扩展javax.swing.plaf.basic中的类,这样可以只是扩展自己需要的内容,不用全部重新编写,后面再看如何自定义laf。
2。UIManager.setLookAndFeel中主要是将laf动态修改。他执行的代码主要如下内容:
1)验证是否支持的laf,主要是执行laf中的isSupportedLookAndFeel方法进行验证。
2)从线程中取LAFState,其中数组中定义的第一个是uidefaults,第二个定义的是systemdefault
3)调用look and feel的initialize方法进行初始化,初始化以后根据初始化的情况设置LAFState的缺省值,调用laf的getDefaults方法。
4)发布look and feel变更的消息
Look And Feel的扩展代码研究,以MetalLookAndFeel为研究目标进行有研究。
1。首先MetalLookAndFeel的实例化
2。调用initialize方法,子类没有定义,跳过
3。制定getDefaults方法,由于BasicLookAndFeel方法定义了一个执行的集合,代码如下
initClassDefaults(table);
initSystemColorDefaults(table);
initComponentDefaults(table);
4。执行的顺序如下,首先定义缺省类,方法体的定义如下
Object[] uiDefaults = {
"ButtonUI", metalPackageName + "MetalButtonUI",
"CheckBoxUI", metalPackageName + "MetalCheckBoxUI",
"ComboBoxUI", metalPackageName + "MetalComboBoxUI",
"DesktopIconUI", metalPackageName + "MetalDesktopIconUI",
"FileChooserUI", metalPackageName + "MetalFileChooserUI",
"InternalFrameUI", metalPackageName + "MetalInternalFrameUI",
"LabelUI", metalPackageName + "MetalLabelUI",
"PopupMenuSeparatorUI", metalPackageName + "MetalPopupMenuSeparatorUI",
"ProgressBarUI", metalPackageName + "MetalProgressBarUI",
"RadioButtonUI", metalPackageName + "MetalRadioButtonUI",
"ScrollBarUI", metalPackageName + "MetalScrollBarUI",
"ScrollPaneUI", metalPackageName + "MetalScrollPaneUI",
"SeparatorUI", metalPackageName + "MetalSeparatorUI",
"SliderUI", metalPackageName + "MetalSliderUI",
"SplitPaneUI", metalPackageName + "MetalSplitPaneUI",
"TabbedPaneUI", metalPackageName + "MetalTabbedPaneUI",
"TextFieldUI", metalPackageName + "MetalTextFieldUI",
"ToggleButtonUI", metalPackageName + "MetalToggleButtonUI",
"ToolBarUI", metalPackageName + "MetalToolBarUI",
"ToolTipUI", metalPackageName + "MetalToolTipUI",
"TreeUI", metalPackageName + "MetalTreeUI",
"RootPaneUI", metalPackageName + "MetalRootPaneUI",
};
table.putDefaults(uiDefaults);
定义了空间委派的UI对象的实现类
5。设置系统属性,也就是颜色的属性,代码如下
MetalTheme theme = getCurrentTheme();
Color control = theme.getControl();
Object[] systemColors = {
"desktop", theme.getDesktopColor(), /* Color of the desktop background */
"activeCaption", theme.getWindowTitleBackground(), /* Color for captions (title bars) when they are active. */
"activeCaptionText", theme.getWindowTitleForeground(), /* Text color for text in captions (title bars). */
"activeCaptionBorder", theme.getPrimaryControlShadow(), /* Border color for caption (title bar) window borders. */
"inactiveCaption", theme.getWindowTitleInactiveBackground(), /* Color for captions (title bars) when not active. */
"inactiveCaptionText", theme.getWindowTitleInactiveForeground(), /* Text color for text in inactive captions (title bars). */
"inactiveCaptionBorder", theme.getControlShadow(), /* Border color for inactive caption (title bar) window borders. */
"window", theme.getWindowBackground(), /* Default color for the interior of windows */
"windowBorder", control, /* ??? */
"windowText", theme.getUserTextColor(), /* ??? */
"menu", theme.getMenuBackground(), /* Background color for menus */
"menuText", theme.getMenuForeground(), /* Text color for menus */
"text", theme.getWindowBackground(), /* Text background color */
"textText", theme.getUserTextColor(), /* Text foreground color */
"textHighlight", theme.getTextHighlightColor(), /* Text background color when selected */
"textHighlightText", theme.getHighlightedTextColor(), /* Text color when selected */
"textInactiveText", theme.getInactiveSystemTextColor(), /* Text color when disabled */
"control", control, /* Default color for controls (buttons, sliders, etc) */
"controlText", theme.getControlTextColor(), /* Default color for text in controls */
"controlHighlight", theme.getControlHighlight(), /* Specular highlight (opposite of the shadow) */
"controlLtHighlight", theme.getControlHighlight(), /* Highlight color for controls */
"controlShadow", theme.getControlShadow(), /* Shadow color for controls */
"controlDkShadow", theme.getControlDarkShadow(), /* Dark shadow color for controls */
"scrollbar", control, /* Scrollbar background (usually the "track") */
"info", theme.getPrimaryControl(), /* ToolTip Background */
"infoText", theme.getPrimaryControlInfo() /* ToolTip Text */
};
table.putDefaults(systemColors);
定义了主题,根据主题进行设定
6。定义了系统的组件属性,代码太多,不在粘贴
组件渲染,以JButton为例进行分析
1。首先JButton执行updateUI
2.通过代码setUI((ButtonUI)UIManager.getUI(this));进行设置,他主要是通过UIManager的getUI取得对象匹配的UI对象,这个地方主要是从laf中的定义进行匹配,然后设置