Repository navigation
Expand file tree
/
Copy pathBot.java
More file actions
90 lines (79 loc) · 2.13 KB
/
Copy pathBot.java
File metadata and controls
90 lines (79 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JTextArea;
public class Bot extends JFrame
{
//typing area
private JTextField txtEnter = new JTextField();
//chat area
private JTextArea txtChat = new JTextArea();
public Bot()
{
//this part deals with how the window looks/behaves when you run the chatbot
//frame attributes
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600, 600);
this.setVisible(true);
this.setResizable(false);
this.setTitle("Java AI");
//txtEnter attributes
txtEnter.setLocation(2,540);
txtEnter.setSize(590,30);
//txtEnter action event
txtEnter.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
String uText = txtEnter.getText();
txtChat.append("You: " + uText + "\n");
txtEnter.setText("");
//these 'if' statements are the logic of the chatbot
//add more 'if' blocks to make it respond to more phrases
if(uText.contains("hi"))
{
botSay("Hello there.");
}
else if(uText.contains("how are you"))
{
botSay("I'm fine, thanks.");
}
else
{
//you can use random number generation, as below, to make the bot's responses seem less static
int decider = (int) (Math.random() * 3 + 1);
if(decider == 1)
{
botSay("I didn't get that.");
}
else if(decider == 2)
{
botSay("Please rephrase that.");
}
else if(decider == 3)
{
botSay("I don't understand.");
}
}
}
});
//txtChat attributes
txtChat.setLocation(20,5);
txtChat.setSize(560,510);
txtChat.setEditable(false);
//add items to frame
this.add(txtEnter);
this.add(txtChat);
}
//this part defines a function to make the bot say stuff
public void botSay(String s)
{
txtChat.append("AI: " + s + "\n");
}
//this part creates a new bot object which does the stuff we defined above
public static void main(String[] args)
{
new Bot();
}
}