• Java 图灵机器人 自动回复


    先去http://www.tuling123.com/申请key

    package com.rjgc13.tuling;
    
    import org.apache.http.HttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    
    public class HttpGetRequest {
        
        public static String get(String url){
            try {
                HttpGet request=new HttpGet(url);
                HttpResponse response=HttpClients.createDefault().execute(request);
                String result="";
                if(response.getStatusLine().getStatusCode()==200){
                    result=EntityUtils.toString(response.getEntity());
                }
                return result;
                
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            }
        }
    }
    package com.rjgc13.tuling;
    
    import net.sf.json.JSONObject;
    
    public class TulingController {
        public static String getTulingRe(String info){
            String url="http://www.tuling123.com/openapi/api?key=********************************&info="+info;
            String tlResult =HttpGetRequest.get(url);
            JSONObject json=JSONObject.fromObject(tlResult);
            tlResult=json.getString("text");
            return tlResult;
        }
    }
    package com.rjgc13.tuling;
    
    import java.util.Properties;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Main extends JFrame {
    
        private static JFrame frame;
        private static JTextArea viewArea;
        private static JTextField viewField;
        private static JButton button1;
        private static JButton button2;
        private static JTextField MyName;
        public static String str="";
        public static void main(String[] args) {
            frame = new JFrame("Chat Room");
            viewArea = new JTextArea(10, 30);
            viewField = new JTextField(50);
            button1 = new JButton("Send");
            button2 = new JButton("Quit");
            MyName = new JTextField();
            MyName.setColumns(9);
            MyName.setText("Saber_Lily:  ");
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(8, 1));
            panel.add(MyName);
            panel.add(button1);
            panel.add(button2);
            JScrollPane sp = new JScrollPane(viewArea);
            viewArea.setLineWrap(true);
            sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
            sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            frame.add("Center", sp);
            frame.add("East", panel);
            frame.add("South", viewField);
            viewField.addKeyListener(new KeyListener() {
    
                public void keyTyped(KeyEvent arg0) {
                    if (arg0.getKeyChar() == '
    ') {
                        String message = "";
                        message = MyName.getText() + viewField.getText();
                        str=str+viewField.getText()+"
    ";
                        String ans = "小U:  ";
                        ans = ans + TulingController.getTulingRe(viewField.getText());
                        viewField.setText("");
                        viewArea.setText(viewArea.getText() + message + "
    ");
                        viewArea.setText(viewArea.getText() + ans + "
    ");
                        viewField.setFocusable(true);
                        viewField.requestFocus();
                    }
    
                }
    
                public void keyReleased(KeyEvent arg0) {
                    // TODO Auto-generated method stub
    
                }
    
                public void keyPressed(KeyEvent arg0) {
                    // TODO Auto-generated method stub
    
                }
            });
            frame.setSize(700, 500);
            frame.setVisible(true);
            // 居中显示
            frame.setLocationRelativeTo(null);
            button1.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent arg0) {
                    String message = "";
                    message = MyName.getText() + viewField.getText();
                    String ans = "小U:  ";
                    ans = ans + TulingController.getTulingRe(viewField.getText());
                    viewField.setText("");
                    viewArea.setText(viewArea.getText() + message + "
    ");
                    viewArea.setText(viewArea.getText() + ans + "
    ");
                    viewField.setFocusable(true);
                    viewField.requestFocus();
    
                }
            });
            button2.addActionListener(new ActionListener() {
    
                public void actionPerformed(ActionEvent arg0) {
                    System.exit(0);
                }
            });
            viewField.setFocusable(true);
            viewField.requestFocus();
            // frame.setFocusable(true);
            // frame.requestFocus();
        }
    }

    源代码下载 链接:http://pan.baidu.com/s/1kU7iQc3 密码:4bbk

  • 相关阅读:
    测试人生 | 40+的年龄50W+的年薪,2线城市入职名企,他曾想放弃测试?
    干货 | 面试过了,如何谈薪资?拿高薪Offer有哪些技巧?
    测试人生 | 三十而立终圆大厂梦,测试开发开启新征程
    测试人生 | 97年双非学历的小哥哥,2线城市涨薪100%,我酸了......
    一道有趣的大厂测试面试题,你能用 Python or Shell 解答吗?
    一道大厂测试开发面试真题,你需要几分钟解答?
    测试人生 | 专科学历入职世界500强企业,二线城市年薪超30W,这个80后小哥哥很赞!
    测试人生 | 00后拿下了名企大厂 offer,这个后浪学习之路全公开
    测试人生 | 三十而已,何需惧怕!年薪40W+涨薪幅度超40%
    使用 Serverless Devs 插件快速部署前端应用
  • 原文地址:https://www.cnblogs.com/zuferj115/p/5340672.html
Copyright © 2020-2023  润新知