-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfriends.java
More file actions
216 lines (163 loc) · 6.95 KB
/
Copy pathfriends.java
File metadata and controls
216 lines (163 loc) · 6.95 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.sql.*;
import java.util.Arrays;
import static javax.swing.JOptionPane.showMessageDialog;
public class friends extends JFrame{
public friends(String id){//id입력받음
super("friend");//프레임 이름
setSize(500,700);//사이즈
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
JButton btn1=new JButton("친구추가");//친구추가 버튼
JTextField frd=new JTextField(10);
frd.setBounds(20,40,340,30);
this.add(frd);
JButton btn2=new JButton("새로고침");//새로고침 버튼
JButton btn3=new JButton("홈");//홈버튼
btn2.setBounds(20,300,100,30);
btn3.setBounds(150,300,100,30);
btn1.setBounds(360,40,100,30);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String frdName=frd.getText();//친구이름 얻기
try {
DatagramSocket ds = new DatagramSocket();//데이터 그램 소켓 생성
InetAddress ia = InetAddress.getByName("localhost");
byte[] bf = frdName.getBytes();
DatagramPacket dp = new DatagramPacket(bf, bf.length, ia, 9993);//데이터 패킷 생성
ds.send(dp);//서버로 친구 아이디 전송
Arrays.fill(bf,(byte)0);
bf=id.getBytes();
dp=new DatagramPacket(bf, bf.length, ia, 9993);
ds.send(dp);//서버로 자신의 아이디 전송
ds.close();
}catch(Exception ex2){
System.out.println(ex2.getMessage());
}
}
});
btn2.addActionListener(new ActionListener() {//새로고침 눌렀을 때
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
new friends(id);
}
});
btn3.addActionListener(new ActionListener() {//홈버튼 눌렀을 때
@Override
public void actionPerformed(ActionEvent e) {
setVisible(false);
Messenger messenger = new Messenger(id);
messenger.jf.setVisible(true);
}
});
this.add(btn1);
setVisible(true);
this.add(btn2);
setVisible(true);
this.add(btn3);
setVisible(true);
PopupMenu pm=new PopupMenu();//팝업메뉴 선언
this.add(pm);
MenuItem mi1=new MenuItem("talk");
MenuItem mi2=new MenuItem("delete friend");
MenuItem mi3=new MenuItem("send file");
MenuItem mi4=new MenuItem("profile");
pm.add(mi1);
pm.add(mi2);
pm.add(mi3);
pm.add(mi4);
try {
DatagramSocket ds = new DatagramSocket();//데이터 그램 소켓 생성
InetAddress ia = InetAddress.getByName("localhost");
byte[] bf = id.getBytes();
DatagramPacket dp = new DatagramPacket(bf, bf.length, ia, 9991);
ds.send(dp);
bf=new byte[1500];
dp=new DatagramPacket(bf,bf.length);
String[] friends= {"", "", "", "", "", "", "",};//친구 이름을 받을 문자열
ds.receive(dp);
int a=(int)bf[0];
Arrays.fill(bf,(byte)0);
for(int j=0;j<a;j++){
ds.receive(dp);
friends[j] = new String(bf).trim();
System.out.println(friends[j]);
Arrays.fill(bf,(byte)0);
}
ds.close();
JList list=new JList(friends);
list.setBounds(20,80,450,200);
list.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
pm.show(friends.this,e.getXOnScreen(),e.getYOnScreen());
mi1.addActionListener(new ActionListener() {//대화하기 눌렀을 때
@Override
public void actionPerformed(ActionEvent e) {
new ChatGUIClient(id,String.valueOf(list.getSelectedValue()));//채팅창 열기
}
});
mi2.addActionListener(new ActionListener() {//친구 삭제 눌렀을 때
@Override
public void actionPerformed(ActionEvent e) {
try {
DatagramSocket ds = new DatagramSocket();//데이터 그램 소켓 생성
InetAddress ia = InetAddress.getByName("localhost");
byte[] bf = String.valueOf(list.getSelectedValue()).getBytes();
DatagramPacket dp = new DatagramPacket(bf, bf.length, ia, 9996);
ds.send(dp);//친구 아이디 전송
Arrays.fill(bf, (byte) 0);
bf = id.getBytes();
dp = new DatagramPacket(bf, bf.length, ia, 9996);
ds.send(dp);//자신의 아이디 전송
ds.close();
} catch(Exception ex2){
System.out.println(ex2.getMessage());
}
}
});
mi3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(list.getSelectedValue()+"3");
}
});
mi4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(list.getSelectedValue()+"4");
}
});
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
list.setVisible(true);
this.add(list);
list.revalidate();
list.repaint();
}catch(Exception ex2){
System.out.println(ex2.getMessage());
}
}
}