march alex's blog
          hello,I am march alex
          posts - 52,comments - 7,trackbacks - 0
          在之前寫過一個TranslateHelper類用于在線翻譯英文單詞。
          我之前寫過一個單詞翻譯大師用于將文件中出現的所有單詞存儲在另一個文件中。存儲的格式如下:
          word: explain
          如:
          apple: 蘋果;蘋果樹;蘋果公司
          banana: 香蕉;芭蕉屬植物;喜劇演員
          我們假設這里獲取單詞的原文件為D:|test_english.txt
          存儲單詞的文件為D:\word_lib.txt

          這次寫的TranslateHelper2類就是在此基礎上編寫的一個英漢詞典的離線版本。
          在此之前我寫了一個WordFinder類用于獲取D:\word_lib.txt下的特定單詞及其解釋(沒有的話返回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類,其詞庫是基于文件D:\word_lib.txt的,如下:
          新增了一個按鍵可在線更新詞庫,即D:\word_lib.txt里面的內容(在現實點按鍵可更新)。

          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("在線時點擊可更新");
                  
                  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 閱讀(409) 評論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 新津县| 巩留县| 尼勒克县| 舟曲县| 公安县| 四平市| 左权县| 齐齐哈尔市| 利川市| 义马市| 平潭县| 大丰市| 德令哈市| 遂溪县| 勃利县| 黑山县| 西宁市| 黄浦区| 大荔县| 余江县| 砚山县| 泰宁县| 枣阳市| 五家渠市| 赤峰市| 永嘉县| 咸宁市| 麻栗坡县| 台湾省| 乌恰县| 德令哈市| 永清县| 崇礼县| 琼中| 陆良县| 三亚市| 大田县| 土默特右旗| 大港区| 綦江县| 河北区|