帮李鑫改的一个小的java程序,一百来行,大概就是一个窗口,来实现智能对话什么的。
在这里记录一下而已,就不贴程序运行图了。也没有添加注释什么的,因为都是一些简单的swing包的东西。
代码如下:
import javax.swing.*; import java.awt.*; import java.awt.event.*; class TalkBrower extends JFrame{ JTextArea jtaA = new JTextArea(); JTextArea jtaC = new JTextArea(); JScrollPane jspA = new JScrollPane(jtaA); JScrollPane jspC = new JScrollPane(jtaC); public void CreateJFrame(String title){ JFrame jf = new JFrame(title); Container container = jf.getContentPane(); container.setBackground(new Color(255,106,106)); jf.setTitle("Talk With Machine"); //final int WIDTH = 500; //final int HEIGHT = 500; //jf.setSize(WIDTH,HEIGHT); jf.setBounds(300, 150, 500, 500); jf.setResizable(false); jf.setLayout(null); jf.setVisible(true); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //设置按键 JButton btnSend=new JButton("发送"); JButton btnClear=new JButton("清除"); JButton btnExit=new JButton("退出"); btnSend.setBounds(250,410,70,40); btnClear.setBounds(330,410,70,40); btnExit.setBounds(410,410,70,40); container.add(btnSend); container.add(btnClear); container.add(btnExit); jtaA.setFont(new Font("Simhei",0,18)); jtaA.setAutoscrolls(true); jspA.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); container.add(jspA); jspA.setBounds(30, 280, 430, 110); jtaC.setLineWrap(true); jtaC.setEditable(true); jtaC.setFont(new Font("Simhei",0,18)); jspC.setBounds(30, 20, 430, 250); jtaC.setAutoscrolls(true); jspC.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); container.add(jspC); jtaC.append("Robot: Hi! I am Robot."+"\n"); btnSend.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ jtaC.setCaretPosition(jtaC.getDocument().getLength()); try{ String Send = jtaA.getText(); jtaC.append("You: "+Send+"\n"); if(Send.indexOf("Hello")!=-1){ jtaC.append("Robot: How is it going !"+"\n"); }else if(Send.indexOf("am happy")!=-1){ jtaC.append("Robot: What happened today !"+"\n"); }else if(Send.indexOf("I need")!=-1){ int size = Send.indexOf("I need"); int beginIndex = size+6; String subSend = Send.substring(beginIndex); jtaC.append("Robot: Are you sure you need "+subSend+"?"+"\n"); }else if(Send.indexOf("I can't")!=-1){ int size=Send.indexOf("I can't"); int beginIndex=size+7; String subSend=Send.substring(beginIndex); jtaC.append("Robot: Perhaps you could "+subSend+"if you tried."+"\n"); }else if(Send.indexOf("Are you")!=-1){ int size=Send.indexOf("Are you"); int beginIndex=size+7; String subSend=Send.substring(beginIndex); jtaC.append("Robot:Why does it matter whether I am"+subSend+"?"+"\n"); }else if(Send.indexOf("What")!=-1){ jtaC.append("Robot:What do you think!"+"\n"); }else if(Send.indexOf("Yes")!=-1){ jtaC.append("Robot:You seem quite sure"+"\n"); }else if(Send.indexOf("I feel")!=-1){ jtaC.append("Robot:Good,tell me more about these feelings"+"\n"); }else if(Send.indexOf("You")!=-1){ jtaC.append("Robot:Why do you say that about me?"+"\n"); }else if(Send.indexOf("Why")!=-1){ jtaC.append("Robot:Why do you think so ?"+"\n"); } else if(Send.indexOf("Beacuse")!=-1){ jtaC.append("Robot:Is that the real reasons ?"+"\n"); }else if(Send.indexOf("I want")!=-1){ int size=Send.indexOf("I want"); int beginIndex=size+6; String subSend=Send.substring(beginIndex); jtaC.append("Robot:Why do you want"+subSend+"?"+"\n"); }else if(Send.indexOf("quit")!=-1){ jtaC.append("Robot:Thank you for talking with me."+"\n"); }else if(Send.indexOf(",")!=-1){ jtaC.append("Robot:Please tell me more."+"\n"); }else if(Send.indexOf("?")!=-1){ jtaC.append("Robot:Please consider whether you can answer your own question."+"\n"); } else{ jtaC.append("Robot: Sorry ,I can't understand what you say..."+ "\n"); } }catch(Exception e){} finally{ jtaA.setText(" "); } } } ); btnClear.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ jtaC.setText(" "); } } ); btnExit.addActionListener( new ActionListener(){ public void actionPerformed(ActionEvent event){ try{ System.exit(0); }catch(Exception e){} } } ); } public static void main(String args[]){ new TalkBrower().CreateJFrame("Talk With Machine"); } }