在之前寫(xiě)過(guò)一個(gè)
TranslateHelper類(lèi)用于在線翻譯英文單詞。
我之前寫(xiě)過(guò)一個(gè)
單詞翻譯大師用于將文件中出現(xiàn)的所有單詞存儲(chǔ)在另一個(gè)文件中。存儲(chǔ)的格式如下:
word: explain
如:
apple: 蘋(píng)果;蘋(píng)果樹(shù);蘋(píng)果公司
banana: 香蕉;芭蕉屬植物;喜劇演員
我們假設(shè)這里獲取單詞的原文件為D:|test_english.txt
存儲(chǔ)單詞的文件為D:\word_lib.txt
這次寫(xiě)的TranslateHelper2類(lèi)就是在此基礎(chǔ)上編寫(xiě)的一個(gè)英漢詞典的離線版本。
在此之前我寫(xiě)了一個(gè)WordFinder類(lèi)用于獲取D:\word_lib.txt下的特定單詞及其解釋?zhuān)](méi)有的話返回null)。
WordFinder.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
public class WordFinder {
public static String find(String word) throws Exception {
String filename = new String("D:\\word_lib.txt");
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line = "";
while((line = reader.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line, ":");
String key = st.nextToken();
if(key.equals(word)) {
return st.nextToken();
}
}
return null;
}
public static void main(String[] args) throws Exception {
String ans = find("apple");
System.out.println(ans);
}
}
下面是TranslateHelper2類(lèi),其詞庫(kù)是基于文件D:\word_lib.txt的,如下:
新增了一個(gè)按鍵可在線更新詞庫(kù),即D:\word_lib.txt里面的內(nèi)容(在現(xiàn)實(shí)點(diǎn)按鍵可更新)。
TranslateHelper2.java
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TranslateHelper2 extends JFrame {
private static final int Width = 500;
private static final int Height = 220;
private static JFrame frame = null;
private static FlowLayout flowLayout = null;
private static JLabel label = null;
private static JTextField wordText = null;
private static JTextField explainText = null;
private static JButton button = null;
private static JButton new_button = null;
public TranslateHelper2() {
frame = new JFrame("Translate Helper");
flowLayout = new FlowLayout(FlowLayout.CENTER);
flowLayout.setHgap(20);
flowLayout.setVgap(30);
frame.setLayout(flowLayout);
label = new JLabel("單詞:");
wordText = new JTextField(10);
explainText = new JTextField(40);
button = new JButton("提交");
new_button = new JButton("在線時(shí)點(diǎn)擊可更新");
frame.add(label);
frame.add(wordText);
frame.add(button);
frame.add(explainText);
frame.add(new_button);
button.addActionListener(new ButtonAction());
new_button.addActionListener(new ButtonAction());
frame.setVisible(true);
frame.setSize(Width, Height);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private class ButtonAction implements ActionListener {
public void actionPerformed(ActionEvent evt) {
Object s = evt.getSource();
//System.out.println("hello");
if(s == button) {
String word = wordText.getText();
try {
String _word = word;
String _explain = WordFinder.find(word);
wordText.setText(_word);
explainText.setText(_explain);
} catch (Exception e) {
e.printStackTrace();
}
} else if(s == new_button) {
try {
TranslateMaster.translateAllLocal("D:\\test_english.txt", "D:\\word_lib.txt");
} catch (Exception e) {
return;
}
}
}
}
public static void main(String[] args) {
new TranslateHelper2();
}
}
posted on 2015-03-11 13:25
marchalex 閱讀(408)
評(píng)論(0) 編輯 收藏 所屬分類(lèi):
java小程序