Java的一个小程序

帮李鑫改的一个小的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");
			}
    }
 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值