最近一直在学习java,本科的时候只是做书上的练习,从来没有做过一个使用的东西。昨天考完java的期末试,最后一道大题虽然磕磕绊绊的做出来了。但总觉得太傻瓜。就联想到google的翻译,看有没有开放的API,没想到广大同胞早以聚集在google的大旗下开发各种有意思的小程序了。今天忍不住花了3个多小时试着做了下,呵呵,虽然有些简陋但终归做出来了。现在把源码发上来,请大家测试和批评。注:可以任意传播啊,呵呵。
/*本程序以eclipse3.4为开发环境
* 一个利用google翻译API实现的英汉双译小程序
* Author:winux email: [email protected]
* date:11,17,2010
* */
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import com.google.api.GoogleAPI;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;
import com.google.api.translate.Language.*;
import com.google.api.translate.Translate.*;
import com.google.api.detect.*;
public class Translateor //extends Frame
{
//-----------------检测语言-------此程序暂不需要故未实现------------------
public Boolean languageDetect(String string) throws Exception
{
if(Detect.execute(string) != null)
{
}
return true;
}
public static void main(String []args)
{
GoogleAPI.setHttpReferrer("http://code.google.com/p/i18n-translator/");
final Frame fr= new Frame("翻译小程序");
GridLayout g1 = new GridLayout(2,1);//设置文本框所在容器面板的布局方式
g1.setVgap(10);//设置两个文本框的间距
Panel panel1 = new Panel(g1);//定义文本框所在面板
final TextArea text1 = new TextArea(null,10,60,TextArea.SCROLLBARS_VERTICAL_ONLY);
final TextArea text2 = new TextArea(null,10,60,TextArea.SCROLLBARS_VERTICAL_ONLY);
final JButton button = new JButton("翻译");
//-------------------------定义单选框框选择语言-------------------------
CheckboxGroup cbg = new CheckboxGroup();
Panel p1 = new Panel();
p1.setLayout(new GridLayout(3,1));
//setLayout(new GridLayout(2,1))
final Checkbox cb1 = new Checkbox("英译汉",cbg,true);
final Checkbox cb2 = new Checkbox("汉译英",cbg,false);
p1.add(cb1);
p1.add(cb2);
p1.add(button);
//--------------------------------------------------------------------
FlowLayout fly = new FlowLayout(FlowLayout.LEFT,5,5);
fly.setHgap(30);
fr.setLayout(fly);
panel1.add(text1);
panel1.add(text2);
panel1.setSize(550, 400);
//panel1.setBounds(10,10, 10,10);
//-------------------------------------------------------------------
fr.add(panel1);
fr.add(p1);
//--------------------------响应窗口关闭动作------------------------------
fr.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//------------------------------------------------------------------------
//------------------------检测语言-----------------------------------------
//------------------------------------------------------------------------
//-------------------响应翻译按钮动作--------------------------------------
ActionListener a1 = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String str1,str2;
str1=text1.getText();
//-------------------------------------------------------------------
JOptionPane jop = new JOptionPane();
jop.setBounds(0, 0, 200, 251);
jop.setLocation(100, 100);
//--------------------------------------------------------------------
if(e.getSource()==button)
{
try {
if(cb1.getState()==true)
{
str2=Translate.execute(str1, Language.ENGLISH,Language.CHINESE_SIMPLIFIED );
text2.setText(str2);
}
if(cb2.getState()==true)
{
str2=Translate.execute(str1,Language.CHINESE_SIMPLIFIED,Language.ENGLISH );
text2.setText(str2);
}
str2=text2.getText();
FileOutputStream fos = new FileOutputStream("result.doc");
BufferedOutputStream bos = new BufferedOutputStream(fos);
OutputStreamWriter osw = new OutputStreamWriter(bos);
osw.write(str2);
osw.flush();
osw.close();
jop.showMessageDialog(fr, "恭喜您,翻译结果已写如word文档","完成",
JOptionPane.INFORMATION_MESSAGE);
}
catch(Exception e1)
{
//e1.printStackTrace();
jop.showMessageDialog(fr, "请输入要翻译的内容","警告",JOptionPane.ERROR_MESSAGE);
//JDialog dialog = jop.createDialog(myF, "关于我们");
jop.setVisible(true);
}
}
}
};
//--------------------------------------------------------------------------------
button.addActionListener(a1);
fr.setSize(600,400);
fr.setVisible(true);
}
}
用到的包我放在附件里,供大家下载