前些天用c#写一个记事本练习的时候,遇到一个问题,在网上检索了一段时间,只找到官方的和另外一篇英文文档。
但是在查看代码的时候发现,英文注释对于自己这种英语差到无极限的人,真是麻烦。
于是有想法,用 Java 和 百度翻译API 结合 正则表达式 写了个翻译注释的小应用(太久没写Java,又比较懒,新建文件没有写)。
还是个小白,第一次发博,希望和大家一起交流交流。
这是在导入百度翻译的jar包后,自己写的一个Jframe,实现翻译功能:
package com.gui;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import spring.BaiduTranslateDemo;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.awt.event.ActionEvent;
import java.awt.Choice;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.FlowLayout;
import javax.swing.JEditorPane;
public class Gui extends JFrame {
private JPanel contentPane;
private JFileChooser filechooser = new JFileChooser();
private JEditorPane editorPane;
private Choice choice;
private String originalstr;
private String afterstr;
private String source;
private String result;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Gui frame = new Gui();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Gui() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel = new JPanel();
contentPane.add(panel, BorderLayout.SOUTH);
JButton btnNewButton = new JButton("\u6253\u5F00");
/*用文件流读取字符串到originalstr,并赋值给afterstr*/
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (filechooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
FileReader fw;
try {
fw = new FileReader(filechooser.getSelectedFile());
int ch = 0;
while ((ch = fw.read()) != -1) {
originalstr = originalstr + (char) ch;
}
editorPane.setText(originalstr);
fw.close();
afterstr = originalstr;
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
});
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
panel.add(btnNewButton);
JButton button_1 = new JButton("\u7FFB\u8BD1");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//感觉下面这段代码写的有点乱七八糟的....
Pattern pattern = Pattern.compile("(//.+)|([/*].+[*/])");
//这个正则表达式弄了挺久的,一直在琢磨怎样让 * 不被作为贪婪法则的变量
Matcher matcher = pattern.matcher(afterstr);
try {
while (true) {
if (matcher.find() == true) {
try {
source = matcher.group();
if (source.endsWith("*/")) {
source = source.substring(2, source.length()-2);
}
else
{
source = source.substring(2, source.length());
}
System.out.println(source);
result = BaiduTranslateDemo.translateToEn(source);
//参考 百度翻译开发者平台 下载的文件,直接调用他的方法返回所需要的字符串
System.out.println(source + ":" + result);
if (result == null) {
System.out.println("翻译出错,参考百度错误代码和说明。");
return;
}
afterstr = afterstr.replace(source, result);
} catch (Exception e1) {
e1.printStackTrace();
}
} else {
break;
}
}
} catch (NullPointerException ex) {
System.out.println("NullPointerException");
}
}
});
panel.add(button_1);
JButton button = new JButton("\u4FDD\u5B58");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (filechooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
FileWriter fw;
try {
fw = new FileWriter(filechooser.getSelectedFile());
fw.write(afterstr);
fw.flush();
fw.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
});
panel.add(button);
JPanel panel_1 = new JPanel();
contentPane.add(panel_1, BorderLayout.CENTER);
panel_1.setLayout(new BorderLayout(0, 0));
//这里添加了一个choice控件,来切换文本框中文本内容
choice = new Choice();
choice.add("original");
choice.add("after");
choice.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (choice.getSelectedItem().equals("original")) {
editorPane.setText(originalstr);
} else {
editorPane.setText(afterstr);
}
}
});
panel_1.add(choice, BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane();
panel_1.add(scrollPane, BorderLayout.CENTER);
editorPane = new JEditorPane();
//下面这个是用来更新 新旧字符串 的内容的,感觉弄两个字符串好像也挺鸡肋的啊?..
editorPane.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent e) {
if (choice.getSelectedItem().equals("original")) {
originalstr = editorPane.getText();
afterstr = originalstr;
}
}
public void removeUpdate(DocumentEvent e) {
if (choice.getSelectedItem().equals("original")) {
originalstr = editorPane.getText();
afterstr = originalstr;
}
}
public void changedUpdate(DocumentEvent e) {
if (choice.getSelectedItem().equals("original")) {
originalstr = editorPane.getText();
afterstr = originalstr;
}
}
});
scrollPane.setViewportView(editorPane);
}
}
大概就这样…反正写了一阵子代码,觉得自己写代码的思维好像不太清晰…哎…还是要加油啊…
最后还是希望大家一块好好学习英语…这种东西写来玩玩就好了…哈哈哈