dictionary.java

来自「用java 实现屏幕取词技术」· Java 代码 · 共 63 行

JAVA
63
字号
/* * Dictionary.java * * Created on 2007-7-4, 18:45:51 * * To change this template, choose Tools | Templates * and open the template in the editor. */package dyno.swing.beans;import java.io.IOException;import java.io.InputStream;import java.util.HashMap;import java.util.logging.Level;import java.util.logging.Logger;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;/** * * @author William Chen */public class Dictionary extends HashMap<String, String> {    private static Dictionary instance;    static {        loadDict();    }    private static void loadDict() {        try {            instance = new dyno.swing.beans.Dictionary();            InputStream input = Dictionary.class.getResourceAsStream("dictionary.xml");            Document root = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);            Element rootElement = root.getDocumentElement();            NodeList list = rootElement.getElementsByTagName("word");            if (list != null) {                for (int i = 0; i < list.getLength(); i++) {                    Element wordElement = (org.w3c.dom.Element) list.item(i);                    String english = wordElement.getAttribute("english");                    String chinese = wordElement.getAttribute("chinese");                    instance.put(english, chinese);                }            }        } catch (ParserConfigurationException ex) {            Logger.getLogger("global").log(Level.SEVERE, null, ex);        } catch (SAXException ex) {            Logger.getLogger("global").log(Level.SEVERE, null, ex);        } catch (IOException ex) {            Logger.getLogger("global").log(Level.SEVERE, null, ex);        }    }    public static Dictionary getInstance() {        return instance;    }}

⌨️ 快捷键说明

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