国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

給Java程序穿花衣

2019-11-18 11:52:49
字體:
來源:轉載
供稿:網友

  經常聽朋友說,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 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下的源代碼根目錄下找到(比如我下載的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" +
  "

上一篇:Java的封裝功能

下一篇:HelloWorld 攻略

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 秦安县| 建水县| 武陟县| 瓮安县| 开原市| 庆元县| 本溪| 常宁市| 巴南区| 临沧市| 万年县| 普兰店市| 镇远县| 宣武区| 时尚| 金湖县| 潜江市| 井冈山市| 高雄县| 林口县| 体育| 伊春市| 修水县| 浪卡子县| 兰西县| 浦北县| 噶尔县| 前郭尔| 同仁县| 黎城县| 湘乡市| 灵山县| 黑山县| 扎鲁特旗| 望奎县| 华容县| 峨边| 积石山| 嵊州市| 永定县| 天峨县|