从http://bbs.csdn.net/wap/topics/390833266借鉴的,感觉不错,可以学习。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
public class MainClass extends JFrame implements ActionListener{
JButton an1,an2;
JTextField bjk;
JTextArea area;
JLabel bq1,bq2;
JPanel p1; //声明所有按钮
public static void main(String[] args){
Write.read(); //应该是读取用户所写数据
//System.out.println("chushihua");
new MainClass();
}
public MainClass(){
an1=new JButton("发送");
an2=new JButton("退出");
bjk=new JTextField(10);//输入框长度
area=new JTextArea();
bq1=new JLabel("这里是[居正]出品的聊天机器人,快跟它打个招呼吧~");
bq2=new JLabel("请输入内容并发送:");
p1=new JPanel();
p1.add(bq2);p1.add(bjk);p1.add(an1);p1.add(an2); //下面四个按钮添加到面板
an1.addActionListener(this);
an1.setActionCommand("go"); //设置变量,让actionperform来选择对应处理函数
an2.addActionListener(this);
an2.setActionCommand("exit");
this.add(bq1,BorderLayout.NORTH);//this指代MainClass初始化
this.add(area); //中间输入版块
this.add(p1,BorderLayout.SOUTH); //底下的四个按钮
this.setTitle("[居正]聊天机器人"); //软件的名称
this.setSize(500,400);
this.setLocation(100,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("go")){
String text=bjk.getText(); //读取输入框
//System.out.println(text);
String texttest=robotedit(); //载入下一个函数
System.out.println("texttest:"+texttest);
area.setText(" 我说:\n"+text+"\n\n [JUZEON]Robot说:\n"+texttest); //在整个文本上的记录
bjk.setText(""); //重置整个数据库
}else if(e.getActionCommand().equals("exit")){
System.exit(0);
}
}
public String robotedit(){
/String re="我还在进步,有些问题回答不上来哦~Sorry~";
try{
for(int i=0;i<Write.wen.length;i++){
String mywen=Write.wen[i];
String tada=Write.da[i]; //循环遍历拆分后的语句
System.out.println(mywen);
System.out.println(tada);
String text=bjk.getText(); //读取用户输入的句子
int bl=text.indexOf(mywen);
if((bl)==(-1)){ //没找到会自动输出
}else{
return tada; //如果b1存在,则输出这个答案
}
}
}catch(Exception ergg){
//return "我还在进步,有些问题回答不上来哦~Sorry~";
}
System.out.println("到了返回这里了~");
return "我还在进步,有些问题回答不上来哦~Sorry~";
}
}
class Write{
static String wen[]=new String[1000];
static String da[]=new String[1000]; //创建两个问答字符串
static FileWriter fw;
static BufferedWriter bw;//定义文件写、字节流写声明
static FileReader fr;
static BufferedReader br;//定义文件读、字节流读声明
static int shu=0;
public static void write(){
try{ //写数据到这个txt文件夹
fw=new FileWriter("./talk.txt");
bw=new BufferedWriter(fw);
bw.write("你好 见到你真高兴~\n");
bw.write("作者 是居正这个BT的家伙创造了我~\n");
bw.write("住 我住在我家~\n");
bw.write("石头 我出布,你输了~\n");
bw.write("剪刀 我出石头,你输了~\n");
bw.write("布 我出剪刀,我赢了~~\n");
bw.write("玩 来玩石头剪刀布吧~\n");
}catch(Exception e){}
finally{
try{
bw.flush(); //强制将缓冲区释放
fw.close();
bw.close();
}catch(Exception e){System.out.println(e.toString());}
}
}
public static void read(){
System.out.println("readStart");
try{
File f=new File("./talk.txt"); //建立该txt
System.out.println("set");
if(f.exists()==false){ //如果文件不存在,则返回错误
write();
System.out.println("write");
}
fr=new FileReader("./talk.txt"); //读取这个文件
br=new BufferedReader(fr);
String s="";
int i=0;
while((s=br.readLine())!=null){
System.out.println(s);
String[] st=s.split(" ");
System.err.println(st[0]);
System.err.println(st[1]);
wen[i]=st[0];
da[i]=st[1];
i++;
System.out.println("No."+i+":"+wen[i]); //把write中句子都放在句子wen[i]和da[i]字符串中
System.out.println("No."+i+":"+da[i]);
}
}catch(Exception e){System.err.println(e.toString());}
}
}