今天对于百度翻译,用java代码编写了一个,代码与界面如下
package com.example.baiduTranslate;
import javax.naming.InitialContext;
import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
public class Gui extends JFrame {
JPanel root;
JLabel label1,label2,showrs;
String rs;
JTextField text1;
JTextField textrs = new JTextField();
JButton zh,en;
public Gui(){
super("中英翻译器");
root=new JPanel();
setContentPane(root);
setLayout(null);
InitGlobalFont(new Font("仿宋", Font.PLAIN, 16));
label1 = new JLabel("输入待翻译的语句: ");
label1.setBounds(25,15,150,60);
root.add(label1);
text1=new JTextField();
text1.setBounds(25,60,200,90);
text1.setBackground(Color.WHITE);
root.add(text1);
label2=new JLabel("翻译结果: ");
label2.setBounds(25,200,90,30);
root.add(label2);
zh=new JButton("中译英");
zh.setBounds(25,160,95,30);
zh.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
rs = Test.zhToen(text1.getText());
textrs.setText(rs);
// textrs=new JTextField(rs);
textrs.setBounds(25,240,250,60);
root.add(textrs);
}
});
root.add(zh);
en=new JButton("英译中");
en.setBounds(130,160,95,30);
en.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
rs = Test.enTozh(text1.getText());
// textrs=new JTextField(rs);
textrs.setText(rs);
textrs.setBounds(25,240,250,60);
root.add(textrs);
}
});
root.add(en);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setBounds(400,300,450,600);
setVisible(true);
}
private static void InitGlobalFont(Font font) {
FontUIResource fontRes = new FontUIResource(font);
for (Enumeration<Object> keys = UIManager.getDefaults().keys();
keys.hasMoreElements(); ) {
Object key = keys.nextElement();
Object value = UIManager.get(key);
if (value instanceof FontUIResource) {
UIManager.put(key, fontRes);
}
}
}
public static void main(String[] args) {
new Gui();
}
}
标签:总结,setBounds,rs,每日,add,new,root,textrs From: https://www.cnblogs.com/baijingyun/p/17892003.html