FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaExercises/Java 2/SimpleChatBot.java at master · biblelamp/JavaExercises · GitHub
biblelamp
/
JavaExercises
Public
Notifications
You must be signed in to change notification settings
Fork
130
Star
153
Code
Issues
1
Pull requests
9
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaExercises
/
Java 2
/
SimpleChatBot.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
88 lines (82 loc) · 2.96 KB
Breadcrumbs
JavaExercises
/
Java 2
/
SimpleChatBot.java
Copy path
File metadata and controls
88 lines (82 loc) · 2.96 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/**
* Java. Chatter: simple chatbot
*
* @author Sergey Iryupin
* @version 0.2.3 dated Jun 17, 2017
*/
import
java
.
awt
.*;
import
java
.
awt
.
event
.*;
import
javax
.
swing
.*;
import
javax
.
swing
.
text
.*;
// for StyledDocument
import
bot
.*;
class
SimpleChatBot
extends
JFrame
implements
ActionListener
{
final
String
TITLE_OF_PROGRAM
=
"Chatter: simple chatbot"
;
final
int
START_LOCATION
=
200
;
final
int
WINDOW_WIDTH
=
350
;
final
int
WINDOW_HEIGHT
=
450
;
final
String
CHB_AI
=
"AI"
;
final
String
BTN_ENTER
=
"Enter"
;
JTextPane
dialogue
;
// area for dialog
JCheckBox
ai
;
// enable/disable AI
JTextField
message
;
// field for entering messages
SimpleBot
sbot
;
// chat-bot class (in bot package)
SimpleAttributeSet
botStyle
;
// style bot text
public
static
void
main
(
String
[]
args
) {
new
SimpleChatBot
();
}
/**
* Constructor:
* Creating a window and all the necessary elements on it
*/
SimpleChatBot
() {
setTitle
(
TITLE_OF_PROGRAM
);
setDefaultCloseOperation
(
EXIT_ON_CLOSE
);
setBounds
(
START_LOCATION
,
START_LOCATION
,
WINDOW_WIDTH
,
WINDOW_HEIGHT
);
// area for dialog
dialogue
=
new
JTextPane
();
dialogue
.
setEditable
(
false
);
dialogue
.
setContentType
(
"text/html"
);
JScrollPane
scrollBar
=
new
JScrollPane
(
dialogue
);
// style for bot messages
botStyle
=
new
SimpleAttributeSet
();
StyleConstants
.
setItalic
(
botStyle
,
true
);
StyleConstants
.
setForeground
(
botStyle
,
Color
.
blue
);
//StyleConstants.setAlignment(botStyle, StyleConstants.ALIGN_RIGHT);
// panel for checkbox, message field and button
JPanel
bp
=
new
JPanel
();
bp
.
setLayout
(
new
BoxLayout
(
bp
,
BoxLayout
.
X_AXIS
));
ai
=
new
JCheckBox
(
CHB_AI
);
ai
.
doClick
();
message
=
new
JTextField
();
message
.
addActionListener
(
this
);
JButton
enter
=
new
JButton
(
BTN_ENTER
);
enter
.
addActionListener
(
this
);
// adding all elements to the window
bp
.
add
(
ai
);
bp
.
add
(
message
);
bp
.
add
(
enter
);
add
(
BorderLayout
.
CENTER
,
scrollBar
);
add
(
BorderLayout
.
SOUTH
,
bp
);
setVisible
(
true
);
sbot
=
new
SimpleBot
();
// creating bot object
}
/**
* Listener of events from message field and enter button
*/
@
Override
public
void
actionPerformed
(
ActionEvent
event
) {
if
(
message
.
getText
().
trim
().
length
() >
0
) {
try
{
StyledDocument
doc
=
dialogue
.
getStyledDocument
();
doc
.
insertString
(
doc
.
getLength
(),
message
.
getText
() +
"
\n
"
,
new
SimpleAttributeSet
());
doc
.
insertString
(
doc
.
getLength
(),
TITLE_OF_PROGRAM
.
substring
(
0
,
9
) +
sbot
.
sayInReturn
(
message
.
getText
(),
ai
.
isSelected
()) +
"
\n
"
,
botStyle
);
}
catch
(
Exception
e
) { }
}
message
.
setText
(
""
);
message
.
requestFocusInWindow
();
}
}
Back
|
FazBrowse Home
|
New Git URL