import java.awt.Color;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
/**
*
* @author tian QQ客户端
*/
public class Client extends Clientframe {
Message receivemsg; // 在线用户之间的聊天信息
Usermessage usermsg; // 用户信息
Socket s;
ObjectOutputStream oos;
String ipaddress; // 服务器IP
ArrayList al = new ArrayList();
String cmd;
boolean flag = false;
/**
* 构造方法
*/
public Client() {
usermsg = new Usermessage();
try {
usermsg.ip = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e3) {
e3.printStackTrace();
}
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//上线时触发,发送用户信息(usermsg)给服务器
jb1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String ipaddress = jtfname2.getText(); // 得到服务器IP
try {
if (flag == false) {
s = new Socket(ipaddress, 9999);
oos = new ObjectOutputStream(s.getOutputStream());
flag = true;
}
} catch (UnknownHostException e2) {
e2.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
try {
usermsg.username = jtfname.getText();
usermsg.password = jtfpwd.getText();
usermsg.flag1 = true; // 登陆是设为true
oos.writeObject(usermsg);
oos.reset();
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
ClientThread ct = new ClientThread();
ct.start();
}
});
// 监听用户下线信息
jfb.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
Usermessage usermsg1 = new Usermessage();
usermsg1.flag1 = false; // 表明用户下线
usermsg1.username = usermsg.username;
usermsg1.password = usermsg.password;
try {
usermsg1.ip = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e2) {
e2.printStackTrace();
}
// 将用户信息发给服务器说明该客户要下线
try {
oos.writeObject(usermsg1);
System.exit(0);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
// 监听双击在线用户列表事件
jli.addMouseListener(new MouseListener() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
JList source = (JList) e.getSource();
Object[] selection = source.getSelectedValues();
cmd = (String) selection[0];
String[] ss = cmd.split("\\s+");
jfaa.setTitle("与" + ss[0] + "聊天");
jfaa.setVisible(true);
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
});
//监听私聊send按钮
jbaa.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ClientUtil cu = new ClientUtil();
Message m = new Message();
m.fromname = usermsg.username;
m.msg = jtabb.getText();
String[] ss = cmd.split("\\s+");
m.toname = ss[0];
m.flag = true;
try {
oos.writeObject(m);
} catch (IOException e1) {
e1.printStackTrace();
}
jtabb.setText("");
// 调用ClientUtil中的dealmessage()方法对信息进行处理
ArrayList msglist = ClientUtil.dealmessage(m.msg);
// 调用ClientUtil中的printmsg()将信息显示到面板上
cu.printmsg(msglist, jtaaa,m);
}
});
// 监听群聊send按钮
jba.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Message m = new Message();
m.fromname = usermsg.username;
m.msg = textPane1.getText();
m.toname = "alluser";
try {
oos.writeObject(m);
} catch (IOException e1) {
// e1.printStackTrace();
}
textPane1.setText("");
}
});
// 监听Transportfile按钮,实现文件传输,只能传输小文件
jbbc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FileDialog f = new FileDialog(jfaa, "文件传送", FileDialog.LOAD);
f.setVisible(true);
String filename = f.getDirectory() + f.getFile();
File file = new File(filename);
FileInputStream fis;
try {
fis = new FileInputStream(file);
int len = (int) file.length();
byte[] data = new byte[len];
fis.read(data);
Message m = new Message();
m.data = new byte[len];
m.fromname = usermsg.username;
String[] ss = cmd.split("\\s+");
m.toname = ss[0];
m.flag = false;
for (int i = 0; i < data.length; i++) {
m.data[i] = data[i];
}
oos.writeObject(m);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
}
});
// 监听"群聊"按钮
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jfa.setVisible(true);
}
});
}
//显示在线用户列表(由服务器传入)
class ClientThread extends Thread {
ObjectInputStream ois;
public void run() {
ClientUtil cu = new ClientUtil();
try {
ois = new ObjectInputStream(s.getInputStream());
while (true) {
Object[] o = (Object[]) ois.readObject();
// 处理“用户名重名”信息
if (o[0] instanceof String) {
String s1 = (String) o[0];
if (s1.equals("用户名重复请重新填写")) {
jtfname3.setText(s1);
} else {
jfb.setTitle(jtfname.getText());
jfb.setVisible(true);
jf.setVisible(false);
}
}
// 处理用户信息
if (o[0] instanceof Usermessage) {
Usermessage u = (Usermessage) o[0];
// 处理用户上线信息
if (u.flag1) {
dlm.clear();
for (int i = 0; i < o.length; i++) {
dlm.addElement(o[i].toString());
}
} else {
// 处理用户下线信息
for (int i = 0; i < dlm.size(); i++) {
String s = dlm.get(i).toString();
String[] ss = s.split("\\s+");
if (ss[0].equals(u.username)) {
dlm.remove(i);
break;
}
}
}
}
// 处理聊天信息
if (o[0] instanceof Message) {
Message u = (Message) o[0];
// 处理群聊信息
if (u.toname.equals("alluser")) {
// 调用ClientUtil中的dealmessage()方法对信息进行处理
ArrayList msglist = ClientUtil.dealmessage(u.msg);
// 调用ClientUtil中的printmsg()将信息显示到面板上
cu.printmsg(msglist, textPane,u);
}
// 处理私聊信息
if (u.toname.equals(usermsg.username)) {
if (u.flag) {
jfaa.setVisible(true);
jfaa.setTitle("与" + u.fromname + "聊天");
// 调用ClientUtil中的dealmessage()方法对信息进行处理
ArrayList msglist = ClientUtil.dealmessage(u.msg);
// 调用ClientUtil中的printmsg()将信息显示到面板上
cu.printmsg(msglist, jtaaa,u);
cmd = u.fromname;
} else {
// 处理接收文件
FileDialog f = new FileDialog(jfaa,
"文件接收", FileDialog.SAVE);
f.setVisible(true);
String filename = f.getDirectory()
+ f.getFile();
FileOutputStream fos = new FileOutputStream(
filename);
byte[] buf = u.data;
fos.write(buf);
fos.close();
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Client c = new Client();
}
}
最近下载更多
微信网友_7004855557083136 LV1
2024年5月22日
cksndh LV4
2023年8月16日
fuyouou LV5
2023年6月29日
yuanchuang LV22
2023年2月14日
liys1234 LV9
2022年6月21日
微信网友_6003487859068928 LV5
2022年6月15日
微信网友_5989987974549504 LV5
2022年6月5日
17岁的孩子想糖吃 LV7
2021年12月31日
111111255 LV2
2021年12月24日
Aoifee LV4
2021年11月10日

最近浏览