47

发布时间 2023-12-05 23:50:42作者: 布吉岛???
package com.example;

import okhttp3.*;
import org.json.JSONException;
import org.json.JSONObject;

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

public class zzz extends JFrame {

private static final String API_KEY = "dzjU0EZolFp0hpYlKD8ckSpk";
private static final String SECRET_KEY = "XbMab4iuLO4dkkKACNDQw5nONYRjTyv4";

private static final OkHttpClient HTTP_CLIENT = new OkHttpClient().newBuilder().build();

private JTextArea inputTextArea;
private JTextArea outputTextArea;

public zzz() {
// Set up the main frame
setTitle("Translation GUI");
setSize(500, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);

// Create components
inputTextArea = new JTextArea(10, 40);
outputTextArea = new JTextArea(10, 40);
outputTextArea.setEditable(false);

JButton translateButton = new JButton("Translate");
translateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
translateText();
}
});

// Set up the layout
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
mainPanel.add(new JScrollPane(inputTextArea), BorderLayout.NORTH);
mainPanel.add(new JScrollPane(outputTextArea), BorderLayout.CENTER);

JPanel buttonPanel = new JPanel();
buttonPanel.add(translateButton);
mainPanel.add(buttonPanel, BorderLayout.SOUTH);

// Add components to the frame
add(mainPanel);

// Make the frame visible
setVisible(true);
}

private void translateText() {
try {
String inputText = inputTextArea.getText();
String accessToken = getAccessToken();
String translationResult = performTranslation(inputText, accessToken);
outputTextArea.setText(translationResult);
} catch (IOException e) {
e.printStackTrace();
outputTextArea.setText("Error during translation. Check the console for details.");
}
}

private String performTranslation(String text, String accessToken) throws IOException {
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"from\":\"en\",\"to\":\"zh\",\"text\":\"" + text + "\"}");
Request request = new Request.Builder()
.url("https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=" + accessToken)
.method("POST", body)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "application/json")
.build();
Response response = HTTP_CLIENT.newCall(request).execute();
return response.body().string();
}

private String getAccessToken() throws IOException {
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "grant_type=client_credentials&client_id=" + API_KEY
+ "&client_secret=" + SECRET_KEY);
Request request = new Request.Builder()
.url("https://aip.baidubce.com/oauth/2.0/token")
.method("POST", body)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.build();
Response response = HTTP_CLIENT.newCall(request).execute();
try {
return new JSONObject(response.body().string()).getString("access_token");
} catch (JSONException e) {
throw new RuntimeException(e);
}
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new zzz();
}
});
}
}