论坛登陆 用户名:  密 码:
设为首页  加入收藏
08年北京名校秋季招生
名牌院校免试入学宽进严出,突破考分限制,名校与你零距离,以下院校按报名先后顺序录取,24小时网上报名覆盖全国
  您现在的位置: 中国教育招生在线 >> IT >> JAVA认证 >> IT正文
Look and Feel---JAVA界面(换肤术)
 作者:佚名     2007-3-14 17:32:05        来源:不详  浏览次数:

 

 

 

 

 

 

 

 

与“Look and Feel”密切相关的是LookAndFeel抽象类和UIManager类。


LookAndFeel类

LookAndFeel是一个抽象类,除了提供了一些static方法,还定义了一些抽象的个性化设置方法来由子类实现。

JDK1.1.3开始,Sun提供了三个LookAndFeel的子类 Javax.Swing.plaf.metal.MetalLookAndFeel、

com.sun.java.swing.plaf.motif.MotifLookAndFeel、

com.sun.java.swing.plaf.Windows. WindowsLookAndFeel。


它们分别提供了“Metal”、“Motif”与“Windows”的界面式样。也就是说,任何基于Swing的界面程序本身都可以使用三种系统提供的皮肤。实际上我们也可以直接或间接继承LookAndFeel类,自己编写一种“皮肤”。


开放源代码的产品Skin Look And Feel 1.2.2

在http://www.l2fprod.com/可以找到它的全部源代码。Skin Look And Feel本身还可以更换http://www.l2fprod.com/提供的各种“皮肤”,让你的程序可以各种“皮肤”示人。


UIManager类


这个类就是Swing界面管理的核心,管理Swing的小应用程序以及应用程序样式的状态。UIManager类提供了下列静态方法用于更换与管理“Look and Feel”:


static void addAuxiliaryLookAndFeel(LookAndFeel laf)

//增加一个“Look And Feel”到辅助的“look and feels”列表

static LookAndFeel[] getAuxiliaryLookAndFeels()

//返回辅助的“look and feels”列表(可能为空)。

static String getCrossPlatformLookAndFeelClassName()

//返回缺省的实现了跨平台的Look and Feel——即Java Look and Feel(JLF)。

static UIManager.LookAndFeelInfo[] getInstalledLookAndFeels()

//返回了在目前已经安装的LookAndFeel的信息。

static LookAndFeel getLookAndFeel()

//返回当前使用的Look and Feel

static String getSystemLookAndFeelClassName()

//返回与当前系统相关的本地系统Look and Feel,如果没有实现本地Look and

Feel则返回缺省的跨平台的Look and Feel。

static void installLookAndFeel(String name, String className)

//创建一个新的Look and Feel并安装到当前系统。

static void installLookAndFeel(UIManager.LookAndFeelInfo info)

//创建一个新的Look and Feel并安装到当前系统。

static boolean removeAuxiliaryLookAndFeel(LookAndFeel laf)

//从辅助的“look and feels”列表删除一个“Look And Feel”

static void setInstalledLookAndFeels(UIManager.LookAndFeelInfo[] infos)

//设置当前的已安装Look and Feel信。

static void setLookAndFeel(LookAndFeel newLookAndFeel)

//设置当前使用的LookAndFeel。

static void setLookAndFeel(String className)

//设置当前使用的LookAndFeel。参数是类名。


下面的源代码可以在Skin Look And Feel 1.2.2下的源代码根目录下找到:

public class Skinit extends javax.swing.JApplet

{

/**

* The main program for the Skinit class

*

* @param args The command line arguments

* @exception Exception Description of Exception

*/

public static void main(String[] args) throws Exception

{

if (args.length == 0) {

printUsage();

}

int mainClassNameIndex = -1;

String gtktheme = null;

String kdetheme = null;

String packtheme = null;


for (int i = 0, c = args.length; i < c; i++) {

if (args[i].equals("-gtk")) {

gtktheme = args[++i];

}

else if (args[i].equals("-kde")) {

kdetheme = args[++i];

}

else if (args[i].equals("-pack")) {

packtheme = args[++i];

}

else {

mainClassNameIndex = i;

break;

}

}


String[] realArgs = new String[args.length - mainClassNameIndex - 1];

for (int i = 0, c = realArgs.length; i < c; i++) {

realArgs[i] = args[mainClassNameIndex + i + 1];

}


// First try to find the class

Class clazz = null;

try {

clazz = Class.forName(args[mainClassNameIndex]);

} catch (ClassNotFoundException e) {

System.err.println("The class " + args[mainClassNameIndex] + "

was not found in the classpath.");

System.exit(1);

} catch (Throwable e) {

e.printStackTrace();

System.exit(1);

}

// if the class exists, get the main method

Method mainMethod = null;

try {

mainMethod = clazz.getMethod("main", new Class[]{String[].class});

} catch (NoSuchMethodException e) {

System.err.println("No method public static void main(String[] args) in " +

clazz.getName());

System.exit(1);

} catch (Throwable e) {

e.printStackTrace();

System.exit(1);

}

// try to make sure the main method is Accessible

try {

mainMethod.setAccessible(true);

} catch (Throwable e) {

}

// main class and main method found, time to load the skin

Skin skin = null;

if (packtheme != null) {

if (SkinUtils.DEBUG) {

System.out.println("Loading themepack " + packtheme);

}

skin = SkinLookAndFeel.loadThemePack(packtheme);

}

else if (gtktheme != null) {

if (kdetheme != null) {

skin = new CompoundSkin(SkinLookAndFeel.loadSkin(gtktheme),

SkinLookAndFeel.loadSkin(kdetheme));

}

else {

skin = SkinLookAndFeel.loadSkin(gtktheme);

}

}

/*

* try to use the user default skin

*/

if (skin == null) {

if (SkinUtils.DEBUG) {

System.out.println("Trying user skin");

}

skin = SkinLookAndFeel.getSkin();

}

if (skin != null) {

SkinLookAndFeel.setSkin(skin);

SkinLookAndFeel lnf = new SkinLookAndFeel();

UIManager.setLookAndFeel(lnf);

UIManager.addPropertyChangeListener(

new PropertyChangeListener() {

public void propertyChange(PropertyChangeEvent event) {

Object newLF = event.getNewValue();

if ((newLF instanceof SkinLookAndFeel) == false) {

try {

UIManager.setLookAndFeel(new SkinLookAndFeel());

} catch (Exception e) {

e.printStackTrace();

}

}

}

});

}

else {

System.out.println("No GTK theme provided, defaulting to application Look And

Feel");

}

try {

mainMethod.invoke(null, new Object[]{realArgs});

} catch (IllegalAccessException e) {

System.err.println("Please make sure the class " + clazz.getName() +

" and the method main(String[] args) are public.");

System.exit(1);

} catch (Throwable e) {

e.printStackTrace();

System.exit(1);

}

}

/**

* Description of the Method

*/

static void printUsage() {

String usage = "Skinit - Skin Look And Feel wrapper\n" +

"Usage: skinit [options] class [args...]\n" +

"\n" +

"where options include:\n" +

"\t-gtk GTK Theme Filename\n" +

"\t-kde KDE Theme Filename\n" +

"\t-pack Theme Pack Filename\n";

System.out.println(usage);

System.exit(1);

}

}


在此程序中使用了Java的反射技术(Reflection)。在JDK文档中有它的详细介绍。

此程序最重要的是在:

……

SkinLookAndFeel.setSkin(skin);

SkinLookAndFeel lnf = new SkinLookAndFeel();

UIManager.setLookAndFeel(lnf);

//这里是将当前的Look and Feel设置为自己定义的SkinLookAndFeel对象。

UIManager.addPropertyChangeListener(

new PropertyChangeListener() {

public void propertyChange(PropertyChangeEvent event) {

Object newLF = event.getNewValue();

if ((newLF instanceof SkinLookAndFeel) == false) {

try {

UIManager.setLookAndFeel(new SkinLookAndFeel());

}

catch (Exception e) {

e.printStackTrace();

}

}

}

});

//监听属性改变事件,不允许用户更改LookAndFeel。即在用户要求改变Look and

Feel时重新将LookAndFeel设置成SkinLookAndFeel。

……

实际上这里只演示了一种皮肤,在http://www.l2fprod.com/有更多的皮肤可以尝试。



责任编辑:lss
  相关新闻
JSFToolbox--用Dreamweaver开发JSF
Java模板引擎Velocity基本语法
Google回应维亚康母的起诉 称YouTube未侵权
Wicket初次接触之HelloWorld
允许内部用户使用Outlook Web Access
如何培养面向对象(OO)的思维方式
快速上手Spring--9.Lookup方法注入
媒体巨头起诉Google和YouTube 赔偿10亿美元
用Apache Velocity模板引擎速造网站(1)
使用Buffalo集成Spring写的一个登录实例
MCDBA 数据库设计学习BLOG
Google回应维亚康母的起诉 称YouTube未侵权
牵手“书生”出师不利 Google身陷盗版泥潭
巨头联手 IBM与Google合推桌面即时可用服务
联想“抛弃”Google 将改用微软的搜索工具
Google中国染指搜书业务 或遭联合对抗
媒体巨头起诉Google和YouTube 赔偿10亿美元
Wicket初次接触之HelloWorld
快速上手Spring--9.Lookup方法注入
如何培养面向对象(OO)的思维方式
  评论
现在有100人对本文发表评论
查看所有评论
 
推  荐
 
100本成功必读热销书
热门招生
  北京文理研修学院   前进大学
  北京明园大学   北京建设大学
  北京邮电大学世纪学院   北方工商管理学院
  联想软件定向委培班   香港数码动画学院
  青年企业管理研修学院   北京华夏管理学院
热门培训
网络化办公专家培训认证 电子科技大学软件学院
软件测试工程师培训认证 北大青鸟十大授权培训
IT硬件工程师培训认证班 北京环球雅思荷兰预科
JAVA开发工程师培训 潜能时代IT服务管理培训
网络信息化工程师培训 清华大学继续教育学院
论坛精选
 
有些细节是男人也该注意的风度!最容易读错的字
某强人手机里保存的30条短信 中国十大高薪职业
最感人的十大韩剧经典台词 嫁给工程师的N个理由
爆强!只有一句话的鬼故事 转贴教你如何做妖精
 女人一定要記住的話 女人最好别嫁给最爱的男人
城市联盟
 大连 上海 天津 广州 西安 深圳  天津  青岛  大连  福州  沈阳  青海  连云港  南京  吉林  厦门  威海  辽宁  呼和浩特
Copyright © 2006   www.edu999.com   All rights reserved. 中国教育招生在线  版权所有
北京市通信管理局[2004]字第552号函    京ICP证040442号