/**
*
* 本系统全称:山寨QQ聊天系统 ( 客户端 )
* 类功能简介:为了减轻服务器端的负载,在本系统中将客户端用户与好友的聊天功能
* 集成到了客户端。这个类只需一个连接即可实现用户与好友之间的聊天
* 作为一个独立的功能模块,该类实现了用户与聊天的全过程控制、容错
* 处理以及自身资源的开辟和释放
*
*/
/*
* FriendChat.java
*
* Created on 2010-6-11, 12:17:24
*/
package cn.zzuli;
import com.sun.awt.AWTUtilities;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/**
*
* 本类为好友聊天界面,集成了全程的控制代码,作为功能模块被移植到其他系统中时
* 只需在调用处实例化该类,并传人一个Socket连接对象,一行代码即可集成该聊天功
* 能。
*
*/
public class FriendChat extends javax.swing.JFrame {
/** Creates new form FriendChat */
private int windowSize = 0;
private boolean doMove = false;
private Thread drag;
private Socket client;//Socket连接对象
private DataOutputStream out;//输出流
private DataInputStream in;//输入流
private insertTextPane setTextPaneString1;
private Sys_Timer sys_Tiemr = new Sys_Timer();//系统时间
private String Message;//消息
private Color docColor = Color.BLACK;//默认字体颜色
private JScrollBar jsp;
private Thread rev;//接收线程
private String send_Message;//发送消息
private int fontSize = 12;//默认字体大小
private boolean canChat = true;//是否可以聊天
private boolean isPIC = false;//是否是表情
private String usingPIC;//当前要发送的表情
private ImageIcon usingFace;//表情对象
private Point pressedPoint;//自定义窗体拖动的Point对象
/**
* 构造方法,接受连接对象,利用该连接与对方通信
* @param client 连接对象
* @param myName 本地用户的昵称
* @param myID 本地用户的ID
* @param i
*/
public FriendChat(Socket client, String myName, String myID, int i) {
initComponents();
setPlace();
facePane.setVisible(false);
jLabel5.setText(myName);
jLabel6.setText(myID);
this.client = client;
setTextPaneString1 = new insertTextPane(jTextPane1);//以上是为图形界面进行的初始化准备
sendStart();//启动消息发送模块
send("Fri_nam" + myName + ":" + myID);//向对方好友发送本地用户的昵称和ID
rev_Thread();//启动接收线程
}
/**
* 封装输出流
*/
public void sendStart() {
try {
out = new DataOutputStream(client.getOutputStream());
} catch (IOException ex) {
setVisible(false);
JOptionPane.showMessageDialog(rootPane, "未能与好友建立可靠连接!");
}
}
/**
* 发送消息
* @param sendMessage
*/
public void send(String sendMessage) {
try {
out.writeUTF(sendMessage);
} catch (IOException ex) {
setTextPaneString1.setDocs("系统提示 " + sys_Tiemr.getSysTime(), docColor, false, fontSize);
setTextPaneString1.setDocs(" 消息发送不成功! \"" + sendMessage + "\"", docColor, false, fontSize);
}
}
/**
* 接收线程
* 内置消息解析器
* 实现对对方消息的接收和解析
*/
public void rev_Thread() {
rev = new Thread() {
@Override
public void run() {
try {
in = new DataInputStream(client.getInputStream());//封装输入流
} catch (Exception e) {
JOptionPane.showMessageDialog(rootPane, "未能与好友建立可靠连接!");
}
while (true) {
try {
Message = in.readUTF();
if (!Message.equals("BYE_BYE")) {
if (Message.startsWith("Fri_nam")) {//接收对方好友的身份信息
int location = Message.indexOf(":");
String friendname = Message.substring(7, location);
String friendID = Message.substring(location + 1, Message.length());
jLabel13.setText(friendname);
jLabel12.setText(friendID);
setTitle("与 " + friendname + " 聊天中");
} else if (Message.startsWith("*PIC*")) {//接收表情
setTextPaneString1.setDocs(jLabel13.getText() + " " + sys_Tiemr.getSysTime(), Color.GREEN, false, fontSize);
setTextPaneString1.insertIcon(new ImageIcon(getClass().getResource(Message.substring(5, Message.length()))));
setTextPaneString1.insert(getFontAttrib("\r\n"));
} else {//接收普通消息
setTextPaneString1.insert(SystemMessage(jLabel13.getText() + " " + sys_Tiemr.getSysTime()));
setTextPaneString1.insert(getFontAttrib(" " + Message));
jsp = jScrollPane1.getVerticalScrollBar();
jsp.setValue(jsp.getMaximum());
}
} else {//对方下线
setTextPaneString1.setDocs("系统提示 " + sys_Tiemr.getSysTime(), Color.GREEN, false, fontSize);
setTextPaneString1.setDocs(" " + jLabel13.getText() + "已离线", Color.RED, false, fontSize);
canChat = false;//对方已下线,不能发送消息
closeChat();
jsp = jScrollPane1.getVerticalScrollBar();
jsp.setValue(jsp.getMaximum());
break;
}
jsp = jScrollPane1.getVerticalScrollBar();
jsp.setValue(jsp.getMaximum());
} catch (Exception e) {
break;
}
try {
Thread.sleep(800);//睡眠800毫秒,降低系统资源占用
jsp = jScrollPane1.getVerticalScrollBar();
jsp.setValue(jsp.getMaximum());
} catch (Exception e) {
}
}
}
};
rev.start();
}
/**
* 该界面的外观风格
*/
public static void setFeel() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());//当前系统风格
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 图形界面的常规代码
* 用于构建整个图形界面
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel2 = new javax.swing.JPanel();
jPanel3 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
JAVA版山寨QQ聊天系统 (380个子文件)
FriendChat.class 59KB
manage.class 37KB
Clienter$1.class 11KB
DataManager.class 9KB
登录中.class 9KB
Clienter.class 9KB
ClientManager.class 7KB
Manager.class 5KB
MyFileTransferHandler.class 4KB
fileSend.class 4KB
登录中$3.class 4KB
登录.class 3KB
FriendChat$1.class 3KB
登录中$4.class 3KB
Clienter$2.class 3KB
TableFill.class 2KB
ClientManager$1.class 2KB
insertTextPane.class 2KB
Manager.class 2KB
Clienter$3.class 2KB
createID.class 1KB
FriendChat$54.class 1KB
logSave.class 1KB
Manager$4.class 1KB
TableFill$1.class 1KB
FriendChat$4.class 1KB
FriendChat$6.class 1KB
FriendChat$5.class 1KB
登录中$2.class 975B
FriendChat$51.class 961B
FriendChat$48.class 961B
FriendChat$44.class 961B
FriendChat$46.class 961B
FriendChat$47.class 961B
FriendChat$40.class 961B
FriendChat$45.class 961B
FriendChat$49.class 961B
FriendChat$52.class 961B
FriendChat$41.class 961B
FriendChat$37.class 961B
FriendChat$42.class 961B
FriendChat$43.class 961B
FriendChat$50.class 961B
FriendChat$53.class 961B
FriendChat$38.class 961B
FriendChat$39.class 961B
FriendChat$36.class 960B
FriendChat$24.class 958B
FriendChat$21.class 958B
FriendChat$34.class 958B
FriendChat$26.class 958B
FriendChat$27.class 958B
FriendChat$15.class 958B
FriendChat$31.class 958B
FriendChat$22.class 958B
FriendChat$25.class 958B
FriendChat$16.class 958B
FriendChat$28.class 958B
FriendChat$19.class 958B
FriendChat$17.class 958B
FriendChat$29.class 958B
FriendChat$23.class 958B
FriendChat$33.class 958B
FriendChat$18.class 958B
FriendChat$20.class 958B
FriendChat$14.class 958B
FriendChat$30.class 958B
FriendChat$35.class 958B
FriendChat$32.class 958B
FriendChat$8.class 956B
FriendChat$2.class 956B
FriendChat$55.class 931B
manage$1.class 927B
登录$1.class 885B
getFilePath.class 816B
manage$2.class 813B
FriendChat$7.class 762B
FriendChat$12.class 755B
FriendChat$11.class 755B
Sys_Timer.class 742B
Sys_Timer.class 742B
manage$19.class 736B
manage$14.class 736B
manage$25.class 736B
manage$17.class 736B
manage$10.class 736B
manage$12.class 736B
manage$21.class 736B
manage$8.class 734B
manage$6.class 733B
manage$26.class 731B
FriendChat$3.class 727B
manage$24.class 727B
manage$23.class 727B
manage$16.class 727B
manage$22.class 727B
manage$27.class 727B
manage$28.class 727B
FriendChat$10.class 723B
FriendChat$13.class 723B
共 380 条
- 1
- 2
- 3
- 4
资源评论
- 冷月无声03232015-06-04很不错,非常适合初学socket
- flash19925202014-07-14对于初学者 来说还是比较实用的
_Myth
- 粉丝: 1
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功