zoukankan      html  css  js  c++  java
  • Java利用图灵机器人接口实现简单的聊天程序

    package test;
    
    import java.awt.EventQueue;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.Timer;
    import javax.swing.JTextField;
    import javax.swing.JTextArea;
    import javax.swing.JLabel;
    import java.awt.Color;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.awt.event.ActionEvent;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    
    public class Test{
    
        private JFrame frame;
        private JTextField textField;
        private JLabel lblNewLabel,lblNewLabel_1;
        private JTextArea textArea;
        /**
         * Launch the application.
         */
        
         private void setTimer(JLabel time){   
                final JLabel varTime = time;   
                Timer timeAction = new Timer(1000, new ActionListener() {          
          
                    public void actionPerformed(ActionEvent e) {       
                        long timemillis = System.currentTimeMillis();
                        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
                        varTime.setText(df.format(new Date(timemillis)));   
                    }      
                });            
                timeAction.start();        
            }   
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        Test window = new Test();
                        window.frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the application.
         */
        public Test() {
            initialize();
        }
        String chat(String quesiton) throws IOException
        {
            String APIKEY="f0feee3416c846a6be5fdc523b372c20";
            String INFO=URLEncoder.encode(quesiton, "utf-8");
            String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO;
            URL getUrl = new URL(getURL);
            HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
            connection.connect();
            
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
            StringBuffer sb = new StringBuffer();
            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);
            return answer;
        }
    
        /**
         * Initialize the contents of the frame.
         */
        private void initialize() {
            frame = new JFrame("Origami");
            frame.setResizable(false);
            frame.setBackground(Color.WHITE);
            frame.setBounds(100, 100, 729, 424);
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.getContentPane().setLayout(null);
            
            ImageIcon icon=new ImageIcon(getClass().getResource("/timg.jpg"));
            lblNewLabel=new JLabel(icon);
            lblNewLabel.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    textArea.setText("干嘛点我...");
                }
            });
            lblNewLabel.setBounds(423, 0, 277, 309);
            frame.getContentPane().add(lblNewLabel);
            
            textField = new JTextField();
            textField.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    String question=textField.getText();
                    try {
                        String answer=chat(question);
                        textArea.setText(answer);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }
            });
            textField.setBounds(14, 322, 395, 24);
            frame.getContentPane().add(textField);
            textField.setColumns(10);
            
            lblNewLabel_1 = new JLabel();
            lblNewLabel_1.setBounds(516, 314, 143, 41);
            frame.getContentPane().add(lblNewLabel_1);
            this.setTimer(lblNewLabel_1);
            
            textArea = new JTextArea();
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
            textArea.setBounds(14, 13, 395, 296);
            frame.getContentPane().add(textArea);
        }
    }
  • 相关阅读:
    Linux下端口被占用,关掉端口占用的方法
    对于STM32F103的USART的通讯调试
    第一次本地代码提交到github
    SPI的学习和ESP8266的SPI通讯测试
    ubuntu下minicom安装和简单设置使用
    ubuntu18.04下stlink的一种安装方法
    使用arm-none-eabi-gdb报错error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
    在麦芒5手机播放音乐时APPS连上后出现自动重启了(请查找:REBOOT关键字)
    一连上蓝牙后,按音量加键,死机(有LOG)
    启动后,只连上蓝牙后,播放音乐,这时按音量,播放,暂停都没功能(按键是有作用的)
  • 原文地址:https://www.cnblogs.com/NDKY9/p/8051579.html
Copyright © 2011-2022 走看看