Java的一个小程序
2015-10-28 21:36
726 查看
帮李鑫改的一个小的java程序,一百来行,大概就是一个窗口,来实现智能对话什么的。
在这里记录一下而已,就不贴程序运行图了。也没有添加注释什么的,因为都是一些简单的swing包的东西。
代码如下:
在这里记录一下而已,就不贴程序运行图了。也没有添加注释什么的,因为都是一些简单的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"); } }
相关文章推荐
- 微信企业号
- 一个点击背景变色的JAVA小程序
- 统计微信分享信息
- 一个倒计时小程序
- 微信分享好友和朋友圈demo
- 企业微信开发(一) 认证、主调、回调
- 微信分享好友和朋友圈demo
- 微信企业号,发消息或获取人员信息时报no privilege to access/modify contact/party/agent
- 腾讯大讲堂:发10亿个红包,微信为啥没崩溃?
- 微信sdk
- 微信擦出插件
- ios微信自动播放音乐
- GPS小程序
- 腾讯封了超10万违规涉赌微信账号
- Java微信插件开发培训—10月28日 晚九点—腾讯课堂
- 微信接口出现“调用支付jsapi缺少参数appid”
- Android ViewPager+Fragment超高仿微信主界面(带底部图标切换动画)
- 微信公众号设置菜单 发布资讯
- 使用input type=file标签进行文件上传时,在安卓手机中的微信浏览器中不能调起相机,但是在苹果手机中的微信浏览器中可以调用相机。解决办法很简单, <input type="file" nam
- 微信签到机制Sign签名制作