上次写了一个基于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 阅读(202)
评论(0) 编辑 收藏 所属分类:
java小程序