java机器人_Java接入图灵机器人,实现与机器人聊天

importjava.awt.FlowLayout;importjava.awt.Font;importjava.awt.GridLayout;importjava.awt.Image;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.beans.EventHandler;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;importjava.net.HttpURLConnection;importjava.net.URL;importjava.net.URLEncoder;importjavax.swing.Icon;importjavax.swing.ImageIcon;importjavax.swing.JButton;importjavax.swing.JColorChooser;importjavax.swing.JDialog;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JPanel;importjavax.swing.JTextArea;importjavax.swing.JTextField;public class图灵机器人 {public图灵机器人() {

}public static void main(String[] args) throwsIOException {

String information= new String("Hello");

String mechine= newString();

JFrame frame= new JFrame("与机器人聊天");

JPanel panel= new JPanel(new GridLayout(3,1));

JPanel questionPanel= new JPanel(newFlowLayout());

JPanel buttonPanel= newJPanel();

JPanel answerPanel= new JPanel(newFlowLayout());

JLabel question= new JLabel("问题");

JTextField enterQuestion= new JTextField(20);

JLabel answer= new JLabel("机器人回答");

JTextArea enterAnswer= new JTextArea(3,25);

JButton submit= new JButton("提交");//ImageIcon imgIcon = new ImageIcon("images/robot.png");//Icon img = imgIcon;//JLabel imgLabel = new JLabel();

frame.setSize(600, 400);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

enterAnswer.setFont(new Font("宋体",Font.BOLD,15));

enterQuestion.setFont(new Font("宋体",Font.BOLD,15));//imgIcon.setImage(imgIcon.getImage().getScaledInstance(35,35,Image.SCALE_DEFAULT));

enterAnswer.setLineWrap(true);//imgLabel.setIcon(img);

questionPanel.add(question);

questionPanel.add(enterQuestion);//answerPanel.add(imgLabel);

answerPanel.add(answer);

answerPanel.add(enterAnswer);

buttonPanel.add(submit);

panel.add(questionPanel);

panel.add(answerPanel);

panel.add(buttonPanel);

frame.add(panel);

submit.addActionListener(newActionListener(){

@Overridepublic voidactionPerformed(ActionEvent e) {

String answer= newString();

String q=enterQuestion.getText();try{

answer=machine(q);

}catch(IOException e1) {

e1.printStackTrace();

}

enterAnswer.setText(answer);

}

});

enterQuestion.addKeyListener(newKeyListener() {

@Overridepublic voidkeyTyped(KeyEvent e) {//TODO Auto-generated method stub

}

@Overridepublic voidkeyPressed(KeyEvent e) {if(e.getKeyCode()==10 || e.getKeyCode()==38) {

String answer= newString();

String q=enterQuestion.getText();try{

answer=machine(q);

}catch(IOException e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}

enterAnswer.setText(answer);

}

}

@Overridepublic voidkeyReleased(KeyEvent e) {//TODO Auto-generated method stub

}

});

}private static String machine(String quesiton) throwsIOException {//接入机器人,输入问题

String APIKEY = "官网给你的api key";

String INFO= URLEncoder.encode(quesiton, "utf-8");//这里可以输入问题

String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" +INFO;

URL getUrl= newURL(getURL);

HttpURLConnection connection=(HttpURLConnection) getUrl.openConnection();

connection.connect();//取得输入流,并使用Reader读取

BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), "utf-8"));

StringBuffer sb= newStringBuffer();

String line= "";while ((line = reader.readLine()) != null) {

sb.append(line);

}

reader.close();//断开连接

connection.disconnect();

String[] ss= new String[10];

String s=sb.toString();

String answer;

ss= s.split(":");

answer= ss[ss.length-1];

answer= answer.substring(1,answer.length()-2);returnanswer;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值