百度翻译更进

public String fanyi(String one,String tow,String beifanyi) throws IOException {
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"from\":\""+one +"\",\"to\":\""+ tow+"\",\"q\":\""+beifanyi +"\"}");
Request request = new Request.Builder()
.url("https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=" + getAccessToken())
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
Response response = HTTP_CLIENT.newCall(request).execute();




////////////////
String jsonStr =" ";
jsonStr=response.body().string();
JSONObject jsonObject = new JSONObject(jsonStr);
JSONObject resultObject = jsonObject.getJSONObject("result");
String dstValue = resultObject.getJSONArray("trans_result").getJSONObject(0).getString("dst");
System.out.println(dstValue);
///////////////
return dstValue;

}
翻译时要先封装成一个完整的类,找到String one,String tow,String beifanyi其中的三个正确的参数,第一个参数代表要翻译的语种,第二个参数代表翻译完的语种,第三个参数代表要翻译的内容,System.out.println(dstValue);
///////////////
return dstValue;
return的结果为翻译完的内容
再以这个fanyi函数的基础之上生成gui界面及其中的翻译按钮,内容设置
 gui界面
package org.example;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class TranslationApp {

private Fanyi translator;

public TranslationApp() {
translator = new Fanyi(); // Replace with your actual Fanyi class instantiation
createAndShowGUI();
}

private void createAndShowGUI() {
JFrame frame = new JFrame("Translation App");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);

JPanel panel = new JPanel();
placeComponents(panel);
frame.add(panel);

frame.setVisible(true);
}

private void placeComponents(JPanel panel) {
panel.setLayout(null);

// JLabel fromLabel = new JLabel("From:");
// fromLabel.setBounds(10, 20, 80, 25);
// panel.add(fromLabel);
//
// JTextField fromText = new JTextField(20);
// fromText.setBounds(100, 20, 165, 25);
// panel.add(fromText);
//
// JLabel toLabel = new JLabel("To:");
// toLabel.setBounds(10, 50, 80, 25);
// panel.add(toLabel);
//
// JTextField toText = new JTextField(20);
// toText.setBounds(100, 50, 165, 25);
// panel.add(toText);

JLabel contentLabel = new JLabel("输入:");
contentLabel.setBounds(10, 80, 80, 25);
panel.add(contentLabel);

JTextField contentText = new JTextField(20);
contentText.setBounds(100, 80, 165, 25);
panel.add(contentText);

// Dropdown menu for translation direction
JLabel directionLabel = new JLabel("选择:");
directionLabel.setBounds(10, 110, 80, 25);
panel.add(directionLabel);

String[] directions = {"中译英", "英译中"};
JComboBox<String> directionDropdown = new JComboBox<>(directions);
directionDropdown.setBounds(100, 110, 165, 25);
panel.add(directionDropdown);

JButton translateButton = new JButton("翻译");
translateButton.setBounds(10, 140, 150, 25);
panel.add(translateButton);

JLabel resultLabel = new JLabel("结果:");
resultLabel.setBounds(10, 170, 80, 25);
panel.add(resultLabel);

JTextField resultText = new JTextField(20);
resultText.setBounds(100, 170, 250, 25);
panel.add(resultText);

translateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// String from = fromText.getText();
// String to = toText.getText();
String content = contentText.getText();
String direction = (String) directionDropdown.getSelectedItem();

// Adjust the function call based on the selected direction
String result = null;
try {
result = (direction.equals("中译英")) ? translator.fanyi("zh", "en", content) :
translator.fanyi("en", "zh", content);
} catch (IOException ex) {
throw new RuntimeException(ex);
}

resultText.setText(result);
}
});
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TranslationApp();
}
});
}
}
posted @ 2023-12-23 23:03  Weebles  阅读(32)  评论(0编辑  收藏  举报