-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatingGui.java
More file actions
122 lines (111 loc) · 4.45 KB
/
ChatingGui.java
File metadata and controls
122 lines (111 loc) · 4.45 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
package network_term;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.awt.Color;
import java.awt.Font;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.Border;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
public class ChatingGui extends JFrame {
private String panelName;
private JTextArea textArea;
private JButton sendButton;
private JButton imgFileButton;
private JTextPane jtp;
private StyledDocument document;
Color talkBackgroundColor = new Color(233, 242, 255);
Color messageSendButtonColor = new Color(209, 206, 206);
Socket socket;
DataOutputStream out;
String msg;
String userID;
String senderID;
public ChatingGui(Socket socket,String userID,String msg) {
this.socket = socket;
this.userID = userID;
this.msg = msg;
setSize(400,630);
setLocation(240,40);
setBackground(talkBackgroundColor);
getContentPane().setLayout(null);
imgFileButton = showImgFileButton();
getContentPane().add(imgFileButton);
sendButton = showSendButton();
getContentPane().add(sendButton);
textArea = new JTextArea(20, 20);
JScrollPane scroller = new JScrollPane(textArea);
scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroller.setBounds(0, 500, 321, 65);
getContentPane().add(scroller);
String arr[] = msg.split(",");
senderID = arr[0];
String sendmsg = arr[1];
StyleContext context = new StyleContext();
document = new DefaultStyledDocument(context);
try{
document.insertString(document.getLength(),"["+senderID+"]"+ sendmsg +"\n",null );
}catch(Exception e){
e.printStackTrace();
}
jtp = new JTextPane(document);
jtp.setBackground(talkBackgroundColor);
JScrollPane scroller2 = new JScrollPane(jtp);
scroller2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
scroller2.setBounds(0, 10, 389, 450);
getContentPane().add(scroller2);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
}
public JButton showImgFileButton() {
JButton imgFileButton = new JButton(new ImageIcon("../image/user.png"));
imgFileButton.setText("File");
imgFileButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
imgFileButton.setBackground(talkBackgroundColor);
Border emptyBorder2 = BorderFactory.createEmptyBorder();
imgFileButton.setBorder(emptyBorder2);
imgFileButton.setFocusPainted(false);
imgFileButton.setBounds(0, 460, 60, 40);
return imgFileButton;
}
public JButton showSendButton() {
JButton sendButton = new JButton(">");
sendButton.setBackground(messageSendButtonColor);
sendButton.setFont(new Font("맑은 고딕", Font.BOLD, 19));
sendButton.setFocusPainted(false);
sendButton.setBounds(320, 500, 56, 63);
sendButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
out = new DataOutputStream(socket.getOutputStream());
String msg = "5"+senderID+","+textArea.getText();
out.writeUTF(msg);
}catch(Exception ek){
ek.printStackTrace();
}
try{
document.insertString(document.getLength(),"["+userID+"]"+ textArea.getText() +"\n",null );
}catch(Exception et){
et.printStackTrace();
}
}
});
return sendButton;
}
}