(swing读书笔记)Swing Look And Feel(4)
2012-03-12 09:20
302 查看
(swing读书笔记)Swing Look And Feel(4)
By cszhao1980八.MetalLookAndFeel的功能增强
Swing对它默认的跨平台L&F提供了功能的增强,即主题(theme)和客户属性(client property)。利用这两种特性,我们可以对L&F进行定制。这其实很容易理解——既然其他L&F的主要目的是为了模拟native样式,我们只需要它像native样式即可,没有必要提供其他定制的方法。从头***一套L&F是个复杂的任务,为了简化这一工作,Swing提供了对Metal L&F的这一简便定制方法。
客户属性比较容易理解,它是可以在运行时刻添加在L&F上的“键/值”对,如:
myComponent.putClientProperty("JTree.lineStyle", "Angled")
通过设置这些属性,可以定制metal L&F的样式,常用的客户属性有:
Component | Property | Data type | Default | Other values |
JInternalFrame | isPalette | Boolean | false | true |
JScrollBar | isFreeStanding | Boolean | true | false |
JSlider | isFilled | Boolean | false | true |
JTree | lineStyle | String | "Horizontal" | "Angled", "None" |
MetalTheme封装了L&F使用的color和font,通过创建自己的主题,就可以改变metal L&F的外观了。
下例创建了自己的theme——继承自DefaultMetalTheme:
// RedTheme.java // import javax.swing.plaf.*; import javax.swing.plaf.metal.*; public class RedTheme extends DefaultMetalTheme { public String getName( ) { return "Mars"; } private final ColorUIResource primary1 = new ColorUIResource(153, 102, 102); private final ColorUIResource primary2 = new ColorUIResource(204, 153, 153); private final ColorUIResource primary3 = new ColorUIResource(255, 204, 204); protected ColorUIResource getPrimary1( ) { return primary1; } protected ColorUIResource getPrimary2( ) { return primary2; } protected ColorUIResource getPrimary3( ) { return primary3; } } |
MetalLookAndFeel.setCurrentTheme(new RedTheme( ));
Metal Theme所能设置的属性很多,见下表:
MetalTheme properties | |||||
Property | Data type | get | is | set | Default value |
acceleratorForeground | ColorUIResource | · | primary1 | ||
acceleratorSelectedForeground | ColorUIResource | · | black | ||
control | ColorUIResource | · | secondary3 | ||
controlDarkShadow | ColorUIResource | · | secondary1 | ||
controlDisabled | ColorUIResource | · | secondary2 | ||
controlHighlight | ColorUIResource | · | white | ||
controlInfo | ColorUIResource | · | black | ||
controlShadow | ColorUIResource | · | secondary2 | ||
controlTextColor | ColorUIResource | · | controlInfo | ||
controlTextFont | FontUIResource | · | Abstract | ||
desktopColor | ColorUIResource | · | primary2 | ||
focusColor | ColorUIResource | · | primary2 | ||
highlightedTextColor | ColorUIResource | · | controlTextColor | ||
inactiveControlTextColor | ColorUIResource | · | controlDisabled | ||
inactiveSystemTextColor | ColorUIResource | · | secondary2 | ||
menuBackground | ColorUIResource | · | secondary3 | ||
menuDisabledForeground | ColorUIResource | · | secondary3 | ||
menuForeground | ColorUIResource | · | black | ||
menuSelectedBackground | ColorUIResource | · | primary2 | ||
menuSelectedForeground | ColorUIResource | · | black | ||
menuTextFont | FontUIResource | · | Abstract | ||
name | String | · | Abstract | ||
primaryControl | ColorUIResource | · | primary3 | ||
primaryControlDarkShadow | ColorUIResource | · | primary1 | ||
primaryControlHighlight | ColorUIResource | · | white | ||
primaryControlInfo | ColorUIResource | · | black | ||
primaryControlShadow | ColorUIResource | · | primary2 | ||
separatorBackground | ColorUIResource | · | white | ||
separatorForeground | ColorUIResource | · | primary1 | ||
subTextFont | FontUIResource | · | Abstract | ||
systemTextColor | ColorUIResource | · | primary1 | ||
systemTextFont | FontUIResource | · | Abstract | ||
textHighlightColor | ColorUIResource | · | primary3 | ||
userTextColor | ColorUIResource | · | black | ||
userTextFont | FontUIResource | · | Abstract | ||
windowBackground | ColorUIResource | · | white | ||
windowTitleBackground | ColorUIResource | · | primary3 | ||
windowTitleFont | FontUIResource | · | Abstract | ||
windowTitleForeground | ColorUIResource | · | black | ||
windowTitleInactiveBackground | ColorUIResource | · | secondary3 | ||
windowTitleInactiveForeground | ColorUIResource | · | black |
Theme的有关color的方法:
protected abstract ColorUIResource getPrimary1( )
protected abstract ColorUIResource getPrimary2( )
protected abstract ColorUIResource getPrimary3( )
protected abstract ColorUIResource getSecondary1( )
protected abstract ColorUIResource getSecondary2( )
protected abstract ColorUIResource getSecondary3( )
protected ColorUIResource getBlack( )
protected ColorUIResource getWhite( )
Theme还提供了一种定制手段,即通过方法:public void addCustomEntriesToTable(UIDefaults table)
例如:
public void addCustomEntriesToTable(UIDefaults table) {
table.put("Tree.openIcon", new ImageIcon("open.gif"));
table.put("Tree.closedIcon", new ImageIcon("closed.gif"))
}
九.定制LookAndFeel
1. 改变Component Properties;2. 改变UI defaults设置;
以上算是静态的修改方法。
3. 动态改变UI defaults,见下例:
UIManager.put("Button.border", tripleBorder); // Custom icons for internal frames UIManager.put("InternalFrame.closeIcon", new ImageIcon("close.gif")); UIManager.put("InternalFrame.iconizeIcon", new ImageIcon("iconify.gif")); |
5. 创建自己的某种Component的ComponentUI,如MyMetalScrollBarUI
(1) 在程序中,将该种Component的defaults值设置为新建class:
UIManager.put("ScrollBarUI", "MyMetalScrollBarUI");
(2) 或者,直接使用JComponent的setUI(ui)方法,单独为一个组件设置UI。
由于组件在构造时,就将UI Delegate设置好了。所以,在动态改变L&F之后,需要通知组件更新UI Delegate,一个常用的方法是调用SwingUtilities.updateComponentTreeUI() ,这个方法会递归的为包含在这个方法内的容器的所有组件更新UI delegate,如:
SwingUtilities.updateComponentTreeUI(getContentPane())
<全文完>
相关文章推荐
- (swing读书笔记)Swing Look And Feel(1)
- (swing读书笔记)Swing Look And Feel(2)
- (swing读书笔记)Swing Look And Feel(3)
- Java swing皮肤(look and feel)大全
- swing look and feel demo
- Java JFC/Swing Look and Feel 创建于展示
- swing look and feel demo
- Swing:LookAndFeel 教程第一篇——手把手教你写出自己的 LookAndFeel
- Java Swing LookAndFeel Demo
- 用各种look and feel打造swing界面
- 一款符合当前主流审美的Swing外观(Look and Feel)_测试版发布
- Swing笔记 1. UIManager , Look and Feel , SwingUtilities
- Java Swing LookAndFeel Demo
- [原创]Swing技巧.5:运行中重设LookAndFeel
- [原创]Swing技巧8:完美的LookAndFeel解决方案
- Java Swing LookAndFeel Demo
- Swing 观感设置(LookAndFeel)(转)
- Swing 可插入外观LookAndFeel浅析
- Swing 皮肤(LookAndFeel)演示 demo
- A Visual Guide to Swing Components (Java Look and Feel)