Look and Feel---JAVA界面(换肤术)


from:
http://www.cnsoft.cn/Exploiture/Programme/JAVA/Applets/200601/6994_3.html

与“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
... {
/***//**
*ThemainprogramfortheSkinitclass
*
*
@paramargsThecommandlinearguments
*
@exceptionExceptionDescriptionofException
*/

publicstaticvoidmain(String[]args)throwsException
...{
if(args.length==0)...{
printUsage();
}

intmainClassNameIndex=-1;
Stringgtktheme
=null;
Stringkdetheme
=null;
Stringpacktheme
=null;

for(inti=0,c=args.length;i<c;i++)...{
if(args[i].equals("-gtk"))...{
gtktheme
=args[++i];
}

elseif(args[i].equals("-kde"))...{
kdetheme
=args[++i];
}

elseif(args[i].equals("-pack"))...{
packtheme
=args[++i];
}

else...{
mainClassNameIndex
=i;
break;
}

}


String[]realArgs
=newString[args.length-mainClassNameIndex-1];
for(inti=0,c=realArgs.length;i<c;i++)...{
realArgs[i]
=args[mainClassNameIndex+i+1];
}


//Firsttrytofindtheclass
Classclazz=null;
try...{
clazz
=Class.forName(args[mainClassNameIndex]);
}
catch(ClassNotFoundExceptione)...{
System.err.println(
"Theclass"+args[mainClassNameIndex]+"
wasnotfoundintheclasspath.");
System.exit(1);
}
catch(Throwablee)...{
e.printStackTrace();
System.exit(
1);
}


//iftheclassexists,getthemainmethod
MethodmainMethod=null;
try...{
mainMethod
=clazz.getMethod("main",newClass[]...{String[].class});
}
catch(NoSuchMethodExceptione)...{
System.err.println(
"Nomethodpublicstaticvoidmain(String[]args)in"+
clazz.getName());
System.exit(
1);
}
catch(Throwablee)...{
e.printStackTrace();
System.exit(
1);
}

//trytomakesurethemainmethodisaccessible
try...{
mainMethod.setAccessible(
true);
}
catch(Throwablee)...{
}

//mainclassandmainmethodfound,timetoloadtheskin
Skinskin=null;
if(packtheme!=null)...{
if(SkinUtils.DEBUG)...{
System.out.println(
"Loadingthemepack"+packtheme);
}

skin
=SkinLookAndFeel.loadThemePack(packtheme);
}

elseif(gtktheme!=null)...{
if(kdetheme!=null)...{
skin
=newCompoundSkin(SkinLookAndFeel.loadSkin(gtktheme),
SkinLookAndFeel.loadSkin(kdetheme));
}

else...{
skin
=SkinLookAndFeel.loadSkin(gtktheme);
}

}

/**//*
*trytousetheuserdefaultskin
*/

if(skin==null)...{
if(SkinUtils.DEBUG)...{
System.out.println(
"Tryinguserskin");
}

skin
=SkinLookAndFeel.getSkin();
}

if(skin!=null)...{
SkinLookAndFeel.setSkin(skin);
SkinLookAndFeellnf
=newSkinLookAndFeel();
UIManager.setLookAndFeel(lnf);
UIManager.addPropertyChangeListener(
newPropertyChangeListener()...{
publicvoidpropertyChange(PropertyChangeEventevent)...{
ObjectnewLF
=event.getNewValue();
if((newLFinstanceofSkinLookAndFeel)==false)...{
try...{
UIManager.setLookAndFeel(
newSkinLookAndFeel());
}
catch(Exceptione)...{
e.printStackTrace();
}

}

}

}
);
}

else...{
System.out.println(
"NoGTKthemeprovided,defaultingtoapplicationLookAnd
Feel");
}

try...{
mainMethod.invoke(
null,newObject[]...{realArgs});
}
catch(IllegalAccessExceptione)...{
System.err.println(
"Pleasemakesuretheclass"+clazz.getName()+
"andthemethodmain(String[]args)arepublic.");
System.exit(
1);
}
catch(Throwablee)...{
e.printStackTrace();
System.exit(
1);
}

}

/***//**
*DescriptionoftheMethod
*/

staticvoidprintUsage()...{
Stringusage
="Skinit-SkinLookAndFeelwrapper "+
"Usage:skinit[options]class[args...] "+
" "+
"whereoptionsinclude: "+
" -gtkGTKThemeFilename "+
" -kdeKDEThemeFilename "+
" -packThemePackFilename ";
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() ... {
publicvoidpropertyChange(PropertyChangeEventevent)...{
ObjectnewLF
=event.getNewValue();
if((newLFinstanceofSkinLookAndFeel)==false)...{
try...{
UIManager.setLookAndFeel(
newSkinLookAndFeel());
}

catch(Exceptione)...{
e.printStackTrace();
}

}

}

}
);

//监听属性改变事件,不允许用户更改LookAndFeel。即在用户要求改变Look and Feel时重新将LookAndFeel设置成SkinLookAndFeel。
……
实际上这里只演示了一种皮肤,在http://www.l2fprod.com/有更多的皮肤可以尝试。

你可能感兴趣的:(java,c,windows,swing,sun)