(swing读书笔记)Swing Look And Feel(4)
(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" |
还有一点要注意的是,更新客户属性后,必须调用repaint()或者revalidate(),因为组件在更新客户属性后,并不会自动更新其外观。
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和font。
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")); |
4. 改变Metal L&F的theme和用户属性;
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(4)相关推荐
- (swing读书笔记)Swing Look And Feel(3)
(swing读书笔记)Swing Look And Feel(3) By cszhao1980 六.UIResource UIResource是个空接口,它的目的是为标识一种身份,即是否为UI Res ...
- (swing读书笔记)Swing Look And Feel(2)
(swing读书笔记)Swing Look And Feel(2) By cszhao1980 三. UI defaults UIDefaults内部维护了一张巨大的Hashtable, 记 ...
- (swing读书笔记)Swing Look And Feel(1)
(swing读书笔记)Swing Look And Feel(1) By cszhao1980 Swing出现之初,就提供一种非常前卫的功能,即PLAF--可拔插的Look And Feel,我们可以 ...
- swing读书笔记转载
(swing读书笔记)Swing Look And Feel(1) http://blog.csdn.net/cszhao1980/article/details/7343524 (swing读书笔记 ...
- (swing读书笔记)Undo/Redo(下)
(swing读书笔记)Undo/Redo(下) By cszhao1980 四.更好的集成--StateEdit 现在,实现Undo功能已经比较简单了: (1) 建立一个"普 ...
- (swing读书笔记)JTable简介(下)
(swing读书笔记)JTable简介(下) by cszhao1980 六.Row 最初,JTable没有提供任何"Row"相关的类来控制Row,与Row相关的大量操作直接坐落在 ...
- (swing读书笔记)JTree简介(下)
(swing读书笔记)JTree简介(下) By cszhao1980 六.TreeCellRenderer 通过JTable一样,JTree也使用一个接口定义它的单元绘制器,该接口仅有一个方法: C ...
- (swing读书笔记)JTree简介(中)
(swing读书笔记)JTree简介(中) By cszhao1980 三.TreeModel(树模型) 同JTable相比,TreeModel接口显得不那么重要.这是Tree的特殊性造成的--Tre ...
- Swing学习系列 -- Swing读书笔记目录
为了便于二次查找,学习,特列出Swing的目录如下 ++ Swing读书笔记 1-1Swing常用的package ++ Swing读书笔记 1-3版面管理器(Layout Menager) 1-3- ...
最新文章
- Silverlight运行原理经典问答。
- javascript +new Date()
- ActiveMQ相关概念
- Linux服务器---安装bind
- Windows下svn服务器安装
- k均值的损失函数_K-Means算法的实现
- IndexAction.java (Java之负基础实战)
- ORACLE分页查询SQL语法——高效的分页
- 地区编码(定位中用到的编码)
- html js3d魔方,40行JavaScript代码实现的3D旋转魔方动画效果
- 山大继续教育计算机答案蒙版,2020年计算机一级Photoshop考试基础自测题与答案...
- 通讯录管理系统(C++)
- 安卓结课作业 音乐播放器 视频播放 游戏 附带源码
- python关闭文件os_python_文件的打开和关闭
- HEVC编码器设计实战-梅奥-专题视频课程
- 软考 | 2019年上半年 软件设计师 下午试卷
- ubuntu18.04 台式机无线网卡驱动安装 Tenda U10 rtl8811CU
- 学校初一模拟赛(2019.4.7)
- ffmpeg批量从视频中提取出mp3音频
- 01 Android Studio学习第一天
热门文章
- EOS系列 - 解决升级EOS2.0 `env.set_proposed_producers_ex unresolveable` 问题
- 触发器实现当一个表中字段改变时同时改变另一个表中字段值
- 爬虫爬取快递100网查快递信息
- TP6------实现连表查询搜索
- python 数据分析 |2. Jupyter Notebook配置和使用
- 有效icon设计的三个属性
- 教你如何搭建编译服务器安装加密系统
- PDF文档物理结构概述
- 全球与中国现代风格沙发市场前景动态及投资方向建议研究报告2022-2028年
- 2019-04-07我破解了中国商标网