⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getresource.java

📁 用JAVA编写的绘图程序 功能简介: 支持存储
💻 JAVA
字号:
package drawsmart.itsv.tool;

import java.util.*;
import java.io.*;
import javax.swing.ImageIcon;
import org.jdom.*;
import org.jdom.input.*;
import java.util.Properties;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class GetResource {

  private org.jdom.Element el;
  private org.jdom.Document doc;

  private static final String filepath=new String("/xmlfile/dramflow.xml");

  /**
   * 环境变量资源表
   */
  private static Hashtable hStrings=new Hashtable();

  /**
   *存储图片资源的哈希表
   */
  private static Hashtable hImages;

  private static Hashtable hClass=new Hashtable();

  public GetResource() {
    try
    {
      String path = System.getProperty("user.dir");
      org.jdom.input.SAXBuilder sb=new org.jdom.input.SAXBuilder();
      //创建文档
      doc=sb.build(new FileInputStream(path+filepath));
      //获得这个文档得跟元素
      el=doc.getRootElement();
      getXMLResource(el);

      Enumeration enumeration=hStrings.keys();
      while(enumeration.hasMoreElements())
      {
        String str1=enumeration.nextElement().toString();
        System.out.println("key:"+str1+"-*-value:"+hStrings.get(str1).toString());
      }
      //System.out.println("ok");
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }

    //获得类
    //getLibClass();
  }
  /**
   *取图片
   * @param name 图片文件名
   * @return
   */
  public static ImageIcon getImage(String name) {
    if (hImages == null) {
      hImages = getImageResource();
    }
    if (hImages != null) {
      return (ImageIcon) hImages.get(name);
    }
    return null;
  }
  /**
   *取XML资源
   * @param name 资源名称
   * @return
   */
  public static String getXMLResource(String name) {
    Object obj = hStrings.get(name);
    if (obj == null) {
      return "";
    }
    else {
      return obj.toString();
    }
  }

  /**
   * 获得图片资源放入哈希表
   * @return
   */
  private static Hashtable getImageResource() {
    Hashtable imagetable = new Hashtable();
    //getClass().getResource(iconPath)
    File imageFile = new File(getXMLResource("imagespath"));
    //System.out.println(System.getProperty("user.dir"));
    File[] files = imageFile.listFiles();
    for (int i = 0; i < files.length; i++) {
      imagetable.put(files[i].getName(), new ImageIcon(files[i].getPath()));
    }
    return imagetable;
  }

  /**
   * 获得图片资源放入哈希表--递归调用
   * @return
   */
  private void getXMLResource(Element el) {
    try {
      java.util.List ls = el.getChildren();
      for (int i = 0; i < ls.size(); i++) {
        Element ele = (Element) ls.get(i);
        if (ele.getChildren().size() > 0) {
          getXMLResource(ele);
        }
        String str1 = ele.getName();
        String str2 = ele.getTextTrim();
        if (str1 != null && str2 != null && !str2.trim().equals("")) {
          hStrings.put(str1, str2);
        }
      }

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

  /**
   * 获得新类
   */
  private void getLibClass()
  {
    String path=hStrings.get("lib").toString();
    File file=new File(path+"/");
    File[] files=file.listFiles();
    GetLibClass getLibClass=new GetLibClass();
    for(int i=0;i<files.length;i++)
    {
      try
      {
        Class fclass=getLibClass.loadClass(files[i].getPath());
        String name=fclass.getName();
        hClass.put(name,fclass);
      }
      catch(Exception classEx)
      {
        classEx.printStackTrace();
      }
    }
  }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -