Packagetest;ImportJava.awt.EventQueue;ImportJavax.swing.ImageIcon;ImportJavax.swing.JFrame;ImportJavax.swing.Timer;ImportJavax.swing.JTextField;ImportJavax.swing.JTextArea;ImportJavax.swing.JLabel;ImportJava.awt.Color;ImportJava.awt.event.ActionListener;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;Importjava.net.HttpURLConnection;ImportJava.net.URL;ImportJava.net.URLEncoder;ImportJava.text.SimpleDateFormat;Importjava.util.Date;Importjava.awt.event.ActionEvent;ImportJava.awt.event.MouseAdapter;Importjava.awt.event.MouseEvent; Public classtest{PrivateJFrame frame; PrivateJTextField TextField; PrivateJLabel Lblnewlabel,lblnewlabel_1; PrivateJTextArea TextArea; /*** Launch the application. */ Private voidSetTimer (JLabel time) {FinalJLabel Vartime =Time ; Timer timeaction=NewTimer (1000,NewActionListener () { Public voidactionperformed (ActionEvent e) {LongTimemillis =System.currenttimemillis (); SimpleDateFormat DF=NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); Vartime.settext (Df.format (NewDate (Timemillis)); } }); Timeaction.start (); } Public Static voidMain (string[] args) {Eventqueue.invokelater (NewRunnable () { Public voidrun () {Try{Test window=NewTest (); Window.frame.setVisible (true); } Catch(Exception e) {e.printstacktrace (); } } }); } /*** Create the application. */ PublicTest () {initialize (); } string Chat (string quesiton)throwsIOException {String APIKEY= "F0feee3416c846a6be5fdc523b372c20"; 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 (); BufferedReader Reader=NewBufferedReader (NewInputStreamReader (Connection.getinputstream (), "Utf-8")); StringBuffer SB=NewStringBuffer (); String Line=""; while(line = Reader.readline ())! =NULL) Sb.append (line); Reader.close (); Connection.disconnect (); String[] SS=NewString[10]; String s=sb.tostring (); String answer; SS= S.split (":"); Answer= Ss[ss.length-1]; Answer= Answer.substring (1,answer.length ()-2); returnanswer; } /*** Initialize The contents of the frame. */ Private voidInitialize () {frame=NewJFrame ("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=NewImageIcon (GetClass (). GetResource ("/timg.jpg")); Lblnewlabel=NewJLabel (icon); Lblnewlabel.addmouselistener (NewMouseadapter () {@Override Public voidmouseclicked (MouseEvent e) {Textarea.settext ("Why do you order me ..."); } }); Lblnewlabel.setbounds (423, 0, 277, 309); Frame.getcontentpane (). Add (Lblnewlabel); TextField=NewJTextField (); Textfield.addactionlistener (NewActionListener () { Public voidactionperformed (ActionEvent e) {String question=Textfield.gettext (); Try{String answer=Chat (question); Textarea.settext (answer); } Catch(IOException E1) {//TODO auto-generated Catch blockE1.printstacktrace (); } } }); Textfield.setbounds (14, 322, 395, 24); Frame.getcontentpane (). Add (TextField); Textfield.setcolumns (10); Lblnewlabel_1=NewJLabel (); Lblnewlabel_1.setbounds (516, 314, 143, 41); Frame.getcontentpane (). Add (Lblnewlabel_1); This. SetTimer (Lblnewlabel_1); TextArea=NewJTextArea (); Textarea.setlinewrap (true); Textarea.setwrapstyleword (true); Textarea.setbounds (14, 13, 395, 296); Frame.getcontentpane (). Add (TextArea); }}
Java uses the Turing man interface to implement a simple chat program