分享
 
 
 

给Java程序穿花衣

王朝java/jsp·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

经常听朋友说,Java编写的程序界面比较单一,不好进行个性化配置。现在让我们一起来了解有关Java界面样式相关类的知识,以及如何用Java写出变幻莫测的用户界面,让Java程序也拥有时髦的换肤功能。

实现原理

Java平台成熟后,设计人员与开发人员就认识到需要连续性好、兼容性好、容易使用的Java程序界面。这时Sun就推出了“Look and Feel”机制迎合这种需求。它提供了一种独特的、与平台无关的程序外观,以及标准的界面行为。它可以在各个平台上使用同一“Look and Feel”,从而缩短设计与开发周期,降低软件使用人员的培训费用。这是“Look and Feel”设计的初衷。现在我们来使用这种特性为Java程序穿上花衣。

要让Java程序具备换肤功能,首先要求有以下JDK版本:

1.Sun JDK1.1.7B/Swing1.1.1 (Windows);

2.Sun JDK1.2.2 (Windows、Solaris、 Linux);

3.Sun JDK 1.3 或以上版本 (Windows)。

我们先来熟悉“Look and Feel”几个相关的类以及API,以便来理解我们的换肤术。与“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 voidaddAuxiliaryLookAndFeel(LookAndFeel laf)

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

static LookAndFeel[]getAuxiliaryLookAndFeels()

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

static StringgetCrossPlatformLookAndFeelClassName()

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

static UIManager.LookAndFeelInfo[]getInstalledLookAndFeels()

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

static LookAndFeelgetLookAndFeel()

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

static StringgetSystemLookAndFeelClassName()

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

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

static voidinstallLookAndFeel(String name, String className)

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

static voidinstallLookAndFeel(UIManager.LookAndFeelInfo info)

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

static booleanremoveAuxiliaryLookAndFeel(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下的源代码根目录下找到(比如我下载的zip包是skinlf-1.2.2-20020611.zip,解压后,在src目录下的Skinit.java)。

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" +

"

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有