forked from YiDaoCai/RemoteDesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientDialogUI.java
More file actions
194 lines (169 loc) · 5.67 KB
/
ClientDialogUI.java
File metadata and controls
194 lines (169 loc) · 5.67 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
package DesktopClientUI;
//学生端界面布局,发文件
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import CommonClass.DesktopRemoteType;
import CommonClass.Information;
import DesktopClientProcess.Client;
public class ClientDialogUI {
private JFrame chat;
private JButton send, raisehand;
private JPanel row1, row2, row3;
private JPanel row2_1;
private JComboBox object;
private JButton sendfile;
private JScrollPane sessionRoll, msgRoll;
private JTextArea session;
private JTextArea msg;
private Client client;
private JFileChooser fc;
public Socket getSocket() {
return client.getSocket();
}
public String getInetAddress() {
return client.getSelfAddress();
}
public ClientDialogUI(Client c) {
client = c;
chat = new JFrame("聊天室");
send = new JButton("发送");
raisehand = new JButton("举手");
row1 = new JPanel();
row2 = new JPanel(new BorderLayout());
row3 = new JPanel(new GridLayout(1, 2));
row2_1 = new JPanel(new FlowLayout());
msg = new JTextArea(5, 25);
msg.setTabSize(4);
msg.setFont(new Font("宋体", Font.BOLD, 16));
msg.setLineWrap(true);
msg.setWrapStyleWord(true);
fc = new JFileChooser();
session = new JTextArea(20, 27);
session.setLineWrap(true);
session.setWrapStyleWord(true);
session.setFont(new Font("宋体", Font.BOLD, 16));
session.append("欢迎来到聊天室\n");
session.setEditable(false);
sessionRoll = new JScrollPane(session);
msgRoll = new JScrollPane(msg);
object = new JComboBox();
object.addItem("至全体");
object.addItem("至老师");
object.setPreferredSize(new Dimension(115, 20));
Image image = (new ImageIcon(this.getClass().getResource("/image/tranFile.png"))).getImage();
Image smallImage = image.getScaledInstance(20, 20, Image.SCALE_FAST);
sendfile = new JButton("交作业", new ImageIcon(smallImage));
sendfile.setPreferredSize(new Dimension(115, 20));
sendfile.setContentAreaFilled(false);
sendfile.setUI(new BasicButtonUI());
sendfile.setMargin(new Insets(0, 0, 0, 0));
sendfile.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (fc.showOpenDialog(chat) == JFileChooser.APPROVE_OPTION) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
File file = fc.getSelectedFile();
try {
Socket filesocket = new Socket(Client
.getServerAddress(),
DesktopRemoteType.ServerFile.getPort());
DataInputStream fis = new DataInputStream(
new BufferedInputStream(
new FileInputStream(file
.getAbsolutePath())));
DataOutputStream ps = new DataOutputStream(
filesocket.getOutputStream());
ps.writeUTF(file.getName());
ps.flush();
ps.writeLong((long) file.length());
ps.flush();
int bufferSize = 8192;
byte[] buf = new byte[bufferSize];
while (true) {
int read = 0;
if (fis != null) {
read = fis.read(buf);
}
if (read <= 0) {
break;
}
ps.write(buf, 0, read);
}
System.out.println("文件发送完成");
addSession("文件\"" + file.getName() + "\"发送完成");
ps.flush();
ps.close();
fis.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}).start();
}
}
});
row2_1.add(object);
row2_1.add(sendfile);
row2_1.setAlignmentX(0);
row2_1.setPreferredSize(new Dimension(0, 25));
chat.setSize(250, 600);
Container con = chat.getContentPane();
con.setLayout(new BorderLayout());
row1.add(sessionRoll);
// row2.setPreferredSize(new Dimension(250, 90));
row2.add(row2_1, BorderLayout.NORTH);
row2.add(msgRoll, BorderLayout.CENTER);
row3.add(raisehand);
row3.add(send);
con.add(row1, BorderLayout.NORTH);
con.add(row2, BorderLayout.CENTER);
con.add(row3, BorderLayout.SOUTH);
raisehand.addActionListener(new Action());
send.addActionListener(new Action());
chat.setResizable(false);
chat.setLocation(1050, 200);
chat.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
chat.setVisible(true);
// setDesktop();
}
public void addSession(String value) {
session.append(value + "\r\n");
JScrollBar jsb = sessionRoll.getVerticalScrollBar();
jsb.setValue(jsb.getMaximum() + 1);
}
public class Action implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource() == send) {
if (msg.getText().length() < 1) return;
if(object.getSelectedItem().equals("至全体")) {
addSession("[Me to all]" + msg.getText());
client.send(new Information("session",client.getSelfAddress(), msg.getText(),"all"));
} else {
addSession("[Me to Server]" + msg.getText());
client.send(new Information("session",client.getSelfAddress(), msg.getText(),"server"));
}
msg.setText(null);
} else if (e.getSource() == raisehand) {
addSession("[Me]我举了手");
client.send(Information.createRaiseHand(client.getSelfAddress()));
}
}
}
}