11.6 百度翻译
实验一:百度机器翻译SDK实验(2024.11.15日完成)
任务一:下载配置百度翻译Java相关库及环境(占10%)。
任务二:了解百度翻译相关功能并进行总结,包括文本翻译-通用版和文本翻译-词典版(占20%)。
任务三:完成百度翻译相关功能代码并测试调用,要求可以实现中文翻译成英文,英文翻译成中文(占30%)。
任务四:完成百度翻译GUI相关功能代码并测试调用,要求可以实现中文翻译成英文,英文翻译成中文(占30%)。
实验总结:(占10%)
页面代码(部分):
package org.example;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TranslationApp extends JFrame implements ActionListener {
private static final String APP_ID = "20241118002205259";
private static final String SECURITY_KEY = "AFv2wcxbN3WXSoXsM3Xw";
private JTextArea sourceTextArea;
private JTextArea translatedTextArea;
private JButton translateButton;
public TranslationApp() {
setTitle("百度翻译");
setSize(800, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBackground(Color.WHITE);
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
JLabel titleLabel = new JLabel("百度翻译");
titleLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 24));
titleLabel.setHorizontalAlignment(JLabel.CENTER);
panel.add(titleLabel, BorderLayout.NORTH);
JPanel contentPanel = new JPanel();
contentPanel.setLayout(new GridLayout(2, 1, 10, 20));
contentPanel.setBackground(Color.WHITE);
JPanel sourcePanel = new JPanel();
sourcePanel.setLayout(new BorderLayout());
sourcePanel.setBackground(Color.WHITE);
JLabel sourceLabel = new JLabel("请输入需要翻译的内容:");
sourceLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
sourceLabel.setForeground(Color.BLACK);
sourcePanel.add(sourceLabel, BorderLayout.NORTH);
sourceTextArea = new JTextArea();
sourceTextArea.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
sourceTextArea.setLineWrap(true);
sourceTextArea.setWrapStyleWord(true);
JScrollPane sourceScrollPane = new JScrollPane(sourceTextArea);
sourceScrollPane.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("输入"),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
sourcePanel.add(sourceScrollPane, BorderLayout.CENTER);
contentPanel.add(sourcePanel);
JPanel translatedPanel = new JPanel();
translatedPanel.setLayout(new BorderLayout());
translatedPanel.setBackground(Color.WHITE);
JLabel translatedLabel = new JLabel("翻译结果:");
translatedLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
translatedLabel.setForeground(Color.BLACK);
translatedPanel.add(translatedLabel, BorderLayout.NORTH);
translatedTextArea = new JTextArea();
translatedTextArea.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
translatedTextArea.setEditable(false);
translatedTextArea.setLineWrap(true);
translatedTextArea.setWrapStyleWord(true);
JScrollPane translatedScrollPane = new JScrollPane(translatedTextArea);
translatedScrollPane.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createTitledBorder("输出"),
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
translatedPanel.add(translatedScrollPane, BorderLayout.CENTER);
contentPanel.add(translatedPanel);
panel.add(contentPanel, BorderLayout.CENTER);
translateButton = new JButton("翻译");
translateButton.setFont(new Font("Microsoft YaHei", Font.PLAIN, 14));
translateButton.setBackground(new Color(52, 152, 219));
translateButton.setForeground(Color.WHITE);
translateButton.setFocusPainted(false);
translateButton.addActionListener(this);
panel.add(translateButton, BorderLayout.SOUTH);
add(panel);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == translateButton) {
String query = sourceTextArea.getText().trim();
if (!query.isEmpty()) {
TransApi api = new TransApi(APP_ID, SECURITY_KEY);
String result = api.getTransResult(query, "auto", "en");
if (result != null) {
int begin = result.indexOf("dst");
if (begin != -1) {
String en = result.substring(begin + 6, result.lastIndexOf("\""));
translatedTextArea.setText(en);
} else {
translatedTextArea.setText("无法获取翻译结果");
}
} else {
translatedTextArea.setText("请求失败");
}
} else {
JOptionPane.showMessageDialog(this, "请输入要翻译的文本", "警告", JOptionPane.WARNING_MESSAGE);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
TranslationApp app = new TranslationApp();
app.setVisible(true);
});
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战