Repository navigation
Expand file tree
/
Copy pathChatUI.java
More file actions
224 lines (194 loc) · 8.24 KB
/
Copy pathChatUI.java
File metadata and controls
224 lines (194 loc) · 8.24 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class ChatUI extends JFrame {
private static final String FAQ_FILE = "faq.txt";
private static final String NEWLY_LEARNED_FILE = "newly_learned.txt";
private Map<String, String> faq = new HashMap<>();
private Set<String> newlyLearned = new HashSet<>();
private JTextArea chatArea;
private JTextField inputField;
private JButton sendButton;
public ChatUI() {
setTitle("Majestic Java ChatBot");
setSize(600, 700);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
initUI();
loadFAQsFromFile();
loadNewlyLearned();
appendBotMessage("Hello! Type 'bye' to exit.");
}
private void initUI() {
// Main panel with padding and background color
JPanel panel = new JPanel(new BorderLayout(10, 10));
panel.setBackground(new Color(25, 25, 35));
panel.setBorder(new EmptyBorder(15, 15, 15, 15));
setContentPane(panel);
// Chat display area
chatArea = new JTextArea();
chatArea.setEditable(false);
chatArea.setLineWrap(true);
chatArea.setWrapStyleWord(true);
chatArea.setFont(new Font("Serif", Font.PLAIN, 16));
chatArea.setBackground(new Color(15, 15, 25));
chatArea.setForeground(Color.WHITE);
JScrollPane scrollPane = new JScrollPane(chatArea);
scrollPane.setBorder(BorderFactory.createLineBorder(new Color(70, 130, 180), 2));
panel.add(scrollPane, BorderLayout.CENTER);
// Input panel with field and button
JPanel inputPanel = new JPanel(new BorderLayout(10, 10));
inputPanel.setBackground(new Color(25, 25, 35));
inputField = new JTextField();
inputField.setFont(new Font("Serif", Font.PLAIN, 18));
inputField.setBackground(new Color(40, 40, 60));
inputField.setForeground(Color.WHITE);
inputField.setCaretColor(Color.WHITE);
inputPanel.add(inputField, BorderLayout.CENTER);
sendButton = new JButton("Send");
sendButton.setFont(new Font("Serif", Font.BOLD, 18));
sendButton.setBackground(new Color(70, 130, 180));
sendButton.setForeground(Color.WHITE);
sendButton.setFocusPainted(false);
inputPanel.add(sendButton, BorderLayout.EAST);
panel.add(inputPanel, BorderLayout.SOUTH);
// Send message when button clicked or Enter pressed
sendButton.addActionListener(e -> sendMessage());
inputField.addActionListener(e -> sendMessage());
}
private void appendUserMessage(String message) {
appendColoredMessage("You: " + message, new Color(135, 206, 250));
}
private void appendBotMessage(String message) {
appendColoredMessage("Bot: " + message, new Color(144, 238, 144));
}
private void appendColoredMessage(String message, Color color) {
chatArea.setForeground(color);
chatArea.append(message + "\n\n");
chatArea.setCaretPosition(chatArea.getDocument().getLength());
chatArea.setForeground(Color.WHITE); // reset to white after appending
}
private void sendMessage() {
String userInput = inputField.getText().trim().toLowerCase();
if (userInput.isEmpty()) return;
appendUserMessage(userInput);
inputField.setText("");
if (userInput.equals("bye")) {
appendBotMessage(faq.getOrDefault("bye", "Goodbye! Have a great day!"));
saveFAQsToFile();
saveNewlyLearned();
disableChat();
return;
}
if (faq.containsKey(userInput)) {
if (newlyLearned.contains(userInput)) {
// Ask if want to change response since newly learned
int option = JOptionPane.showConfirmDialog(this,
"I currently respond with:\n\"" + faq.get(userInput) + "\"\nDo you want to change it?",
"Edit Response", JOptionPane.YES_NO_OPTION);
if (option == JOptionPane.YES_OPTION) {
String newResponse = JOptionPane.showInputDialog(this, "What should I say instead?");
if (newResponse != null && !newResponse.trim().isEmpty()) {
faq.put(userInput, newResponse.trim());
appendBotMessage("Thanks! I've updated my response.");
} else {
appendBotMessage("No changes made.");
}
} else {
appendBotMessage("Okay, I'll keep my response.");
}
newlyLearned.remove(userInput);
saveFAQsToFile();
saveNewlyLearned();
} else {
appendBotMessage(faq.get(userInput));
}
} else {
// New input: learn response for first time
String newResponse = JOptionPane.showInputDialog(this,
"I don't know how to respond to that. What should I say?");
if (newResponse != null && !newResponse.trim().isEmpty()) {
faq.put(userInput, newResponse.trim());
newlyLearned.add(userInput);
appendBotMessage("Got it! I'll remember that.");
saveFAQsToFile();
saveNewlyLearned();
} else {
appendBotMessage("Okay, maybe next time.");
}
}
}
private void disableChat() {
inputField.setEnabled(false);
sendButton.setEnabled(false);
}
private void loadFAQsFromFile() {
File file = new File(FAQ_FILE);
if (!file.exists()) {
// Default FAQs
faq.put("hi", "Hello! How can I help you?");
faq.put("what is your name", "I'm a Java chatbot.");
faq.put("how are you", "I'm doing great! What about you?");
faq.put("good", "Well that's nice, wanna talk?");
faq.put("bye", "Goodbye! Have a great day!");
return;
}
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = FaqFile.parseLine(line);
if (parts != null && !parts[0].trim().isEmpty()) {
faq.put(parts[0].trim().toLowerCase(), parts[1].trim());
}
}
} catch (IOException e) {
appendBotMessage("Failed to load FAQs from file.");
}
}
private void saveFAQsToFile() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FAQ_FILE))) {
for (Map.Entry<String, String> entry : faq.entrySet()) {
writer.write(FaqFile.formatLine(entry.getKey(), entry.getValue()));
writer.newLine();
}
} catch (IOException e) {
appendBotMessage("Failed to save FAQs to file.");
}
}
private void loadNewlyLearned() {
File file = new File(NEWLY_LEARNED_FILE);
if (!file.exists()) return;
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
String line;
while ((line = reader.readLine()) != null) {
String key = FaqFile.clean(line).toLowerCase();
if (!key.isEmpty()) newlyLearned.add(key);
}
} catch (IOException e) {
appendBotMessage("Failed to load newly learned inputs.");
}
}
private void saveNewlyLearned() {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(NEWLY_LEARNED_FILE))) {
for (String input : newlyLearned) {
writer.write(input);
writer.newLine();
}
} catch (IOException e) {
appendBotMessage("Failed to save newly learned inputs.");
}
}
public static void main(String[] args) {
// Use a nice modern look and feel
try {
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception ignored) {
}
SwingUtilities.invokeLater(() -> {
ChatUI chatUI = new ChatUI();
chatUI.setVisible(true);
});
}
}