-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatbotGUI.java
More file actions
182 lines (136 loc) · 5.84 KB
/
ChatbotGUI.java
File metadata and controls
182 lines (136 loc) · 5.84 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
package chatbot.infra;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import chatbot.infra.Chatbot;
public class ChatbotGUI extends JFrame {
static private Chatbot nowChatbot;
static private JFrame nowGUIFrame;
static private JTextField inputTextBox;
static private JTextPane chatHistoryPane;
static private JScrollPane scroll;
static private JButton psuButton;
public ChatbotGUI(Chatbot nowChatbot) {
this.nowChatbot = nowChatbot;
/*
* Task 1: Make the interface prettier!
*
* As you can see, this graphical interface (GUI) is not pretty. Please
* modify the following codes to move the components around or change
* their appearances, such as color or size, to make the interface
* looks nicer. Please explain what you did in your video.
*/
//create the frame of chatbot
nowGUIFrame = new JFrame();
nowGUIFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
nowGUIFrame.setVisible(true);
nowGUIFrame.setResizable(false);
nowGUIFrame.setLayout(null);
nowGUIFrame.setSize(550, 550);
nowGUIFrame.setTitle("Chatbot "+nowChatbot.getBotName()+"");
//create JTextPane
chatHistoryPane = new JTextPane();
nowGUIFrame.add(chatHistoryPane);
chatHistoryPane.setSize(500, 400);
chatHistoryPane.setLocation(2, 2);
chatHistoryPane.setBackground(Color.DARK_GRAY);
//create JTextField
inputTextBox = new JTextField();
nowGUIFrame.add(inputTextBox);
inputTextBox.setSize(500, 30);
inputTextBox.setLocation(2, 410);
inputTextBox.addActionListener(new InputTextListener(inputTextBox, chatHistoryPane, this));
/* Task 2: Connect the "Send" button to the chatbot and show the
* response on the chat pane
*
* For now, when the user clicks the "Send" button, the GUI only shows
* a message "Send" to the chat pane. Please modify the
* actionPerformed() method in the ButtonListener class so that the
* code will execute the following three steps each time the button
* is clicked:
* (1) Pass the message that the user typed in the input box (i.e.,
* the "user message") to the chatbot and receive its response
* (i.e., the "bot response").
* (2) Display "[USER NAME]: [user message]" in the chat pane.
* (3) Display "[BOT NAME]: [bot response]" in the chat pane.
*
* [Hint] You can take a look at the actionPerformed() in the
* InputTextListener class.
*/
//create JButton
psuButton = new JButton("Send", null);
nowGUIFrame.add(psuButton);
psuButton.setBounds(2, 450, 240, 40);
psuButton.addActionListener(new ButtonListener(inputTextBox, chatHistoryPane, this));
}
public ChatbotGUI() {
}
public Chatbot getChatbot() {
return nowChatbot;
}
public static void appendToPane(JTextPane nowPane, String senderName, String message, Color color){
String nowMsg = senderName+": "+message+"\n";
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, color);
aset = sc.addAttribute(aset, StyleConstants.FontFamily, "monospaced");
aset = sc.addAttribute(aset, StyleConstants.FontSize, 16);
aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);
int len = nowPane.getDocument().getLength();
nowPane.setCaretPosition(len);
nowPane.setCharacterAttributes(aset, false);
nowPane.replaceSelection(nowMsg);
}
}
class ButtonListener implements ActionListener{
private ChatbotGUI chatbotUtil;
private JTextField nowInputTextBox;
//private JTextField nowInputTextBox;
private JTextPane nowChatHistoryPane;
public ButtonListener(JTextField inputTextBox, JTextPane chatHistoryPane, ChatbotGUI chatbotUtil) {
this.chatbotUtil = chatbotUtil;
nowInputTextBox = inputTextBox;
nowChatHistoryPane = chatHistoryPane;
//nowUserName = userName;
}
@Override
public void actionPerformed(ActionEvent e) {
String nowInputText = nowInputTextBox.getText();
if (!nowInputText.trim().isEmpty()) {
ChatbotGUI.appendToPane(nowChatHistoryPane, chatbotUtil.getChatbot().getUserName(), nowInputText, Color.BLUE);
String nowChatbotResponse = chatbotUtil.getChatbot().getResponse(nowInputText);
ChatbotGUI.appendToPane(nowChatHistoryPane, chatbotUtil.getChatbot().getBotName(), nowChatbotResponse, Color.BLACK);
nowInputTextBox.setText("");
}
}
}
class InputTextListener implements ActionListener{
private ChatbotGUI chatbotUtil;
private JTextField nowInputTextBox;
private JTextPane nowChatHistoryPane;
public InputTextListener(JTextField inputTextBox, JTextPane chatHistoryPane, ChatbotGUI chatbotUtil) {
this.chatbotUtil = chatbotUtil;
nowInputTextBox = inputTextBox;
nowChatHistoryPane = chatHistoryPane;
//nowUserName = userName;
}
@Override
public void actionPerformed(ActionEvent e) {
String nowInputText = nowInputTextBox.getText();
ChatbotGUI.appendToPane(nowChatHistoryPane, chatbotUtil.getChatbot().getUserName(), nowInputText, Color.WHITE);
String nowChatbotResponse = chatbotUtil.getChatbot().getResponse(nowInputText);
ChatbotGUI.appendToPane(nowChatHistoryPane, chatbotUtil.getChatbot().getBotName(), nowChatbotResponse, Color.GREEN);
nowInputTextBox.setText("");
}
}