march alex's blog
          hello,I am march alex
          posts - 52,comments - 7,trackbacks - 0
          上次寫(xiě)了一個(gè)基于iciba的英漢翻譯,這次在原來(lái)的基礎(chǔ)上加了一個(gè)窗口,實(shí)現(xiàn)了TranslateHelper類(lèi)。
          效果如下:


          代碼如下:
          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) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): java小程序
          主站蜘蛛池模板: 新化县| 乌鲁木齐市| 平山县| 晋州市| 肥东县| 乌什县| 柯坪县| 攀枝花市| 绵阳市| 禄丰县| 特克斯县| 石阡县| 化州市| 红安县| 盱眙县| 彭水| 什邡市| 大洼县| 海南省| 胶南市| 墨竹工卡县| 连平县| 仙居县| 道孚县| 肃北| 溧阳市| 榆林市| 新宾| 怀仁县| 全椒县| 平乐县| 桃江县| 旬邑县| 三穗县| 义马市| 上杭县| 新河县| 中超| 富宁县| 临武县| 含山县|