首页 > 编程语言 >Java调用Google的翻译实例

Java调用Google的翻译实例

时间:2022-12-11 20:00:13浏览次数:58  
标签:Google Java java GroupLayout private 实例 swing new javax


今天在网上乱逛,一不小心发现这个Google翻译,用Java调用的实例,把它拷来看了一下,挺不错的!
原文是英语和法语的互相翻译,我改成中文和英语的,但是好像中文翻译英文时有点问题,翻译不了!其他的都可以用的。
下面是一个截图:



源代码:

package test;

/*
* TranslatorFrame.java
*
* Created on 03 February 2008, 19:47
*/

import com.google.api.translate.Language;
import com.google.api.translate.Translate;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author rich
*/
public class TranslatorFrame extends javax.swing.JFrame {

/** Creates new form TranslatorFrame */
public TranslatorFrame() {
initComponents();
setLocationRelativeTo(null);
}

private void translate() {
try {
String languageFrom = null;
String languageTo = null;
if (chineseToEnglishMenuItem.isSelected()) {
languageFrom = Language.CHINESE_SIMPLIFIED;
languageTo = Language.ENGLISH;
} else if (englishToChineseMenuItem.isSelected()) {
languageFrom = Language.ENGLISH;
languageTo = Language.CHINESE_SIMPLIFIED;
}
toTextArea.setText(Translate.translate(fromTextArea.getText().trim(), languageFrom, languageTo));
} catch (Exception ex) {
Logger.getLogger(TranslatorFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

buttonGroup1 = new javax.swing.ButtonGroup();
jPanel2 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
fromTextArea = new javax.swing.JTextArea();
jPanel3 = new javax.swing.JPanel();
jScrollPane2 = new javax.swing.JScrollPane();
toTextArea = new javax.swing.JTextArea();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenu3 = new javax.swing.JMenu();
englishToChineseMenuItem = new javax.swing.JRadioButtonMenuItem();
chineseToEnglishMenuItem = new javax.swing.JRadioButtonMenuItem();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Google翻译");
getContentPane().setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.PAGE_AXIS));

fromTextArea.setColumns(20);
fromTextArea.setLineWrap(true);
fromTextArea.setRows(5);
fromTextArea.setWrapStyleWord(true);
fromTextArea.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
fromTextAreaKeyPressed(evt);
}
});
jScrollPane1.setViewportView(fromTextArea);

javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
.addContainerGap())
);

getContentPane().add(jPanel2);

toTextArea.setColumns(20);
toTextArea.setEditable(false);
toTextArea.setLineWrap(true);
toTextArea.setRows(5);
toTextArea.setWrapStyleWord(true);
jScrollPane2.setViewportView(toTextArea);

javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
.addContainerGap())
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel3Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 93, Short.MAX_VALUE)
.addContainerGap())
);

getContentPane().add(jPanel3);

jMenu1.setText("文件");

jMenuItem1.setText("退出");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);

jMenuBar1.add(jMenu1);

jMenu3.setText("语言");

buttonGroup1.add(englishToChineseMenuItem);
englishToChineseMenuItem.setSelected(true);
englishToChineseMenuItem.setText("英语 > 简体中文");
jMenu3.add(englishToChineseMenuItem);

buttonGroup1.add(chineseToEnglishMenuItem);
chineseToEnglishMenuItem.setText("简体中文 > 英语");
jMenu3.add(chineseToEnglishMenuItem);

jMenuBar1.add(jMenu3);

setJMenuBar(jMenuBar1);

pack();
}// </editor-fold>

private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
System.exit(0);
}

private void fromTextAreaKeyPressed(java.awt.event.KeyEvent evt) {
if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
translate();
evt.consume();
}
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new TranslatorFrame().setVisible(true);
}
});
}

// Variables declaration - do not modify
private javax.swing.ButtonGroup buttonGroup1;
private javax.swing.JRadioButtonMenuItem englishToChineseMenuItem;
private javax.swing.JRadioButtonMenuItem chineseToEnglishMenuItem;
private javax.swing.JTextArea fromTextArea;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu3;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextArea toTextArea;
// End of variables declaration

}



上面的代码用到了Google提供的一个jar,下载地址:
​​​google-api-translate-java-0.4.jar​






标签:Google,Java,java,GroupLayout,private,实例,swing,new,javax
From: https://blog.51cto.com/u_5488952/5928583

相关文章

  • Java 面经手册·小傅哥.pdf
     这是一本以面试题为入口讲解Java核心内容的技术书籍,书中内容极力的向你证实代码是对数学逻辑的具体实现。当你仔细阅读书籍时,会发现Java中有大量的数学知识,包括:扰动......
  • 你不知道的Javascript 上 1作用域是什么 1.1编译原理
    什么是编译程序中一段源代码经过parse(解析)、transform(转换)、generate(生成)三个阶段,这三个阶段统称为编译编译过程详解分词/词法分析解析1分词:Tokenizing分词......
  • (收藏)Java 6 JVM参数选项大全(中文版)
    ​​http://kenwublog.com/docs/java6-jvm-options-chinese-edition.htm​​​Java6JVM参数选项大全(中文版)......
  • java中继承的内存分析
    本文主要讲述java中继承的内存分析。示例代码如下:publicclassEncapsulationTest{publicstaticvoidmain(String[]args){Sonson=newSon();......
  • Java Optional
    Java8新增的一个容器对象,相当于在对象上面进行了包装,并提供了一些操作对象的实用方法。一些方法的使用示例:@Testpublicvoidoptional(){//empty......
  • Java异常学习
    P77Error和Exception什么是异常实际工作中,遇到的情况不可能是非常完美的。比如:你写的某个模块,用户输入不一定符合你的要求、你的程序要打开某个文件,这个文件可能不存在......
  • JavaScript笔记 - 防抖与节流
    防抖与节流目录防抖与节流1.防抖2.节流在进行窗口操作或者输入框操作时,如果事件处理函数触发频率过高,会加重浏览器和服务器的负担。此时可以使用防抖和节流的方式来减......
  • Java面向对象学习
    P60什么是面向对象面向过程&面向对象面向过程思想步骤清晰简单,第一步做什么,第二步做什么...面对过程适合处理一些较为简单的问题面向对象思想物以类聚,分类的......
  • IDEA创建Java Web项目
    ✅作者简介:热爱国学的Java后端开发者,修心和技术同步精进。......
  • JAVA实现爬虫
    1.项目搭建2.导入相关依赖<dependency><!--jsoup只能解析网页tika能解析视频网站--><groupId>org.jsoup</groupId><artifactId>jsoup</artif......