march alex's blog
          hello,I am march alex
          posts - 52,comments - 7,trackbacks - 0
          上次寫了一個基于iciba的英漢翻譯,這次在原來的基礎上加了一個窗口,實現了TranslateHelper類。
          效果如下:


          代碼如下:
          import java.awt.FlowLayout;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;

          import javax.swing.JButton;
          import javax.swing.JFrame;
          import javax.swing.JLabel;
          import javax.swing.JTextField;


          public class TranslateHelper extends JFrame {
               private static final int Width = 500;
               private static final int Height = 200;
               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;
               
               public TranslateHelper() {
                   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("提交");
                  
                  frame.add(label);
                  frame.add(wordText);
                  frame.add(button);
                  frame.add(explainText);
                  
                  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");
                          String word = wordText.getText();
                          
                          try {
                              String _word = EnglishChineseTranslater.getWordName(word);
                              String _explain = EnglishChineseTranslater.getTranslation(word);
                              wordText.setText(_word);
                              explainText.setText(_explain);
                              
                          } catch (Exception e) {
                              e.printStackTrace();
                          }
                      }
              }
              public static void main(String[] args) {
                  new TranslateHelper();
              }
          }
          posted on 2015-03-08 18:47 marchalex 閱讀(208) 評論(0)  編輯  收藏 所屬分類: java小程序
          主站蜘蛛池模板: 盘山县| 大同市| 赤壁市| 开原市| 丰都县| 阿坝| 维西| 吉木乃县| 伊宁县| 巴马| 古蔺县| 汝州市| 邓州市| 闻喜县| 平罗县| 丰城市| 宜兰市| 措美县| 颍上县| 元朗区| 昌邑市| 彝良县| 南皮县| 曲靖市| 武强县| 金华市| 安宁市| 聂拉木县| 漯河市| 塔城市| 赣州市| 岑溪市| 安新县| 抚松县| 开鲁县| 修水县| 龙江县| 福鼎市| 恩施市| 潜江市| 灌云县|