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;
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);
jb1.addActionListener(
new
ActionListener() {
public
void
actionPerformed(ActionEvent e) {
String ipaddress = jtfname2.getText();
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
;
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) {
}
});
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(
""
);
ArrayList msglist = ClientUtil.dealmessage(m.msg);
cu.printmsg(msglist, jtaaa,m);
}
});
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) {
}
textPane1.setText(
""
);
}
});
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"
)) {
ArrayList msglist = ClientUtil.dealmessage(u.msg);
cu.printmsg(msglist, textPane,u);
}
if
(u.toname.equals(usermsg.username)) {
if
(u.flag) {
jfaa.setVisible(
true
);
jfaa.setTitle(
"与"
+ u.fromname +
"聊天"
);
ArrayList msglist = ClientUtil.dealmessage(u.msg);
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();
}
}