- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
Latest commit
87 lines (67 loc) · 3.18 KB
/
Copy pathGUI.java
File metadata and controls
87 lines (67 loc) · 3.18 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
/*
* class GUI
* Interface of chat program
*/
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.io.PrintWriter;
importjava.io.BufferedWriter;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.io.File;
importorg.jdesktop.xswingx.*;
importjavax.swing.*;
importjava.awt.GridLayout;
publicclassGUIimplementsActionListener/* ActionListener is an abstract interface, consisting of one method actionPerformed(ActionEvent e). This method must be over-written in any classes implementing ActionListener. This method is then associated with an event (JButton, JFrame, etc.), which then "listens" (i.e. makes sure something is done when a button is pressed, etc) */ {
privateMessageManager_messageManager;
privateGridLayoutg = newGridLayout(3,4); /*GridLayout implements LayoutManager and Serializable. It must implement LayoutManager, because LayoutManager allows you to manage the layout (whoa!) of a JFrame. GridLayout then must over-write all of the methods in LayoutManager, and then is allowed to create a layout for a Container, such as JFrame. Serializable allows any class implementing it, to be serialized. Serialization is the storing of an object in its state, in a transmittable file. If an object is serialized, it can be opened on different computers in the same way. GridLayout needs to implement this so it doesn't come out wonky on different computers. */
privateJFrameframe = newJFrame();
privateJTextAreadisplay = newJTextArea();
privateJButtonsend = newJButton("Send");
privateJTextAreawriteHere = newJTextArea();
privateStringmessage;
publicGUI() { }
// GUI is created by Session
publicGUI (MessageManagermessageManager) {
_messageManager = messageManager;
make();
}
publicvoidactionPerformed(ActionEventevent){
message = writeHere.getText();
writeHere.setText("");
if (message.trim().length() > 0) {
// Remove trailing whitespace
StringmsgContent = message.replaceFirst("\\s+$", "");
// Add user's message to GUI
display.append("You: "+ msgContent + "\n");
// Pass it on to MessageManager
_messageManager.sendMessage(msgContent);
}
}
privatevoidmake(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setLocationRelativeTo( null );
frame.setLayout(g);
frame.getContentPane().add(newJScrollPane(display));
display.setLineWrap(true);
display.setWrapStyleWord(true);
display.setEditable(false);
frame.getContentPane().add(newJScrollPane(writeHere));
writeHere.setLineWrap(true);
writeHere.setWrapStyleWord(true);
frame.getContentPane().add(send);
PromptSupport.setPrompt("Type Here!",writeHere);
send.addActionListener(this);
frame.setVisible(true);
}
publicStringgetMessage(){
returnmessage;
}
publicJTextAreagetDisplay() {
returndisplay;
}
publicstaticvoidmain (String[] args){
GUItestGUI = newGUI();
}
}