栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现智能对话机器人自动聊天+语音秒回

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现智能对话机器人自动聊天+语音秒回

技术说明:

1. 调用青云课人工智能聊天API接口

请求地址http://api.qingyunke.com/api.php
请求方式GET字符编码utf-8
请求示例http://api.qingyunke.com/api.php?key=free&appid=0&msg=你好

2. 下载jacob.jar实现文本转语音

需要资源或代码有疑问的可加我的技术讨论q群1005611884

3. 使用Java Swing实现简单的聊天界面

【完整代码】

public class Demo {
    public static void main(String[] args){
        JFrame jf = new JFrame("深夜聊天室");
        jf.setSize(580, 600);
        jf.setLocation(150, 150);

        jf.add(new MyPanel());
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.setResizable(false);
        jf.setVisible(true);
    }
}
    public static class MyPanel extends JLayeredPane{
        ArrayList allMsg = new ArrayList<>();
        JTextArea jTextRecode = new JTextArea();
        public MyPanel(){
            JTextArea jTextMsg = new JTextArea();
            jTextMsg.setBorder(new BevelBorder(1,Color.gray,Color.gray));
            jTextMsg.setLocation(20,400);
            jTextMsg.setSize(500, 100);
            this.add(jTextMsg);

            JButton btnSend = new JButton();
            btnSend.setText("发送");
            btnSend.setBounds(450,500,70,30);

            btnSend.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    String me =jTextMsg.getText();
                    jTextMsg.setText("");
                    allMsg.add(me);
                    jTextRecode.setText(listTostr(allMsg));
                    repaint();
                    new Thread(){
                        @Override
                        public void run() {
                            URL url = null;
                            try {
                                url = new URL("http://api.qingyunke.com/api.php?key=free&appid=0&msg="+me);
                                InputStream is = url.openStream();
                                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                                String content = br.readLine();
                                JSONObject obj = JSONObject.fromObject(content);
                                content = obj.getString("content");
                                allMsg.add("菲菲:"+content);
                                jTextRecode.setText(listTostr(allMsg));
                                repaint();
                                speak(content);
                            } catch (MalformedURLException ex) {
                                ex.printStackTrace();
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }.start();
                }
            });
            this.add(btnSend);

            jTextRecode.setBorder(new BevelBorder(1,Color.gray,Color.gray));
            jTextRecode.setLocation(20,20);
            jTextRecode.setFont(new Font("楷体",0,18));
            jTextRecode.setSize(500, 400);
            this.add(jTextRecode);
        }


        //集合转字符串 
        public static String listTostr(ArrayList list){
            String content = "";
            for(int i=0;i 

需要资源或代码有疑问的可加我的技术讨论q群1005611884 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/885619.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号