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
{
/**
* 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 " +
"Usage: skinit [options] class [args...] " +
" " +
"where options include: " +
" -gtk GTK Theme Filename " +
" -kde KDE Theme Filename " +
" -pack Theme Pack Filename ";
System.out.println(usage);
System.exit(1);
}
}
{
/**
* 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 " +
"Usage: skinit [options] class [args...] " +
" " +
"where options include: " +
" -gtk GTK Theme Filename " +
" -kde KDE Theme Filename " +
" -pack Theme Pack Filename ";
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();
}
}
}
});
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/有更多的皮膚可以嘗試。
相關文章
- QT自定義精美換膚介面QT
- 主流技術之網易雲換膚方案
- 網頁常見的換膚技術網頁
- look look C#7C#
- Android換膚實現技術思路總結Android
- 程式碼換膚術(一)——C#和VB (轉)C#
- iOSSpin(換膚)iOS
- winform 換膚元件ORM元件
- Android 換膚指南Android
- jsp換皮膚JS
- 深入淺出換膚相關技術以及如何實現
- 聊一聊前端換膚前端
- 多套主題怎麼靈動換膚?APP換膚方案詳細解析!APP
- look-into-oracle-redoOracle
- very important -have a LookImport
- 換膚功能實現過程
- 換膚功能接入及原理
- 寶塔皮膚切換PHP版本PHP
- look up the sql_textSQL
- DB2LOOK 命令DB2
- 微信小程式實現換膚功能微信小程式
- antd原始碼解析——線上換膚功能原始碼
- MyEclipse更換主題皮膚Eclipse
- Android 常用換膚方式以及原理分析Android
- 寶塔皮膚切換預設PHP版本PHP
- js效果——頁面如何讓換皮膚JS
- localStorage網頁換膚程式碼例項網頁
- JS實現網頁換膚功能效果JS網頁
- Qt QtCharts給QChartView換膚,換背景色新增背景圖片QTView
- iOS換膚功能的簡單處理框架iOS框架
- Flutter高階(二)——國際化與換膚Flutter
- db2look相關操作DB2
- 多國家美膚方案SDK,打造全球膚色適配技術
- win10瀏覽器皮膚怎麼更換_win10瀏覽器皮膚更換如何設定Win10瀏覽器
- Flutter主題切換——讓你的APP也能一鍵換膚FlutterAPP
- 第12周-Microwave皮膚圖形介面基礎
- Android 無縫換膚深入瞭解與使用Android
- webpack構建下換膚功能的實現思路Web