📄 fchatuser.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;
import java.net.*;
import java.io.*;
import javax.swing.event.*;
import java.awt.geom.*;
public class FChatUser implements Runnable
{
AChatFrame C;
Vector v=new Vector(0,1);//收集登录用户的用户名
Vector V=new Vector(0,1);//收集暂时离开用户的用户名
FChatInfo CI;
Socket socket;
ObjectInputStream in;
ObjectOutputStream out;
String clientMode="offline";//设置在线状态
Thread UserThread;
String StopUser;//用户屏蔽的名单
String CC="";
public FChatUser()
{
C=new AChatFrame("天使聊天室",this);
}
public static void main(String args[])//主线程
{
FChatUser fc=new FChatUser();
}
public void setClientModeOn()//设置再线状态并启动线程
{
clientMode="online";
UserThread=new Thread(this);
UserThread.start();
}
public void setClientModeOff()//关闭线程
{
try
{
socket.close();
}
catch(IOException e)
{}
clientMode="offline";
C.C.B.M.L.UserLabel.setText("在线用户数:"+0);
C.C.B.M.L.U.UserList.removeAll();
C.C.B.M.L.U.UserList.setListData(new Vector(0));
}
public String getClientMode()//获得再线状态
{
return clientMode;
}
public void login(AChatFrame acf)//用户登录方法
{
String s=CCardFrame.hashmapnicheng.get(acf.C.A.D.L.TF2.getText()).toString();//取得用户的昵称
CI=new FChatInfo(s,"login",null,"","",null,0,null);
try
{
socket=new Socket("127.0.0.1",1001);
out=new ObjectOutputStream(socket.getOutputStream());
in=new ObjectInputStream(socket.getInputStream());
out.writeObject(CI);
while(true)
{
out.flush();
try
{
CI=(FChatInfo)in.readObject();
}
catch(ClassNotFoundException cnfe)
{}
if(CI.getCommand().equals("login"))//如果接收到的命令是登录
{
v=CI.getVector();
JLabel W=new JLabel("***用户: "+s+" 欢迎您进入天使聊天室***");
W.setFont(new Font("宋体",Font.PLAIN,13));
W.setForeground(new Color(64,0,128));
C.C.B.M.T.TA.add(W);
C.C.B.M.L.UserLabel.setText("在线用户数:"+CI.getVector().size());
C.C.B.M.L.U.UserList.removeAll();
C.C.B.M.L.U.UserList.setListData(v);
setClientModeOn();
break;
}
if(CI.getCommand().equals("Cannot in"))//如果接收到的命令是你已经被赶出聊天室
{
DWrong13Dialog D=new DWrong13Dialog(C,"Error",true);
return;
}
if(CI.getCommand().equals("tryagain"))//如果接收到的命令是用户名已经存在
{
DWrong4Dialog D=new DWrong4Dialog(C,"Error",true);
return;
}
}
}
catch(IOException e)//如果服务器没有开启
{
DWrong10Dialog D=new DWrong10Dialog(C,"Error",true);
CI.setCommand("ServerOff");
return;
}
}
public void logout()//用户登出时调用的方法
{
String S=CCardFrame.hashmapnicheng.get(C.C.A.D.L.TF2.getText()).toString();
if(clientMode=="online")
{
CI=new FChatInfo(S,"logout",null,null,"",null,0,null);
try
{
out.writeObject(CI);
}
catch(IOException e)
{}
setClientModeOff();
}
}
public void stopuser()//用户被屏蔽时调用的方法
{
String S=CCardFrame.hashmapnicheng.get(C.C.A.D.L.TF2.getText()).toString();
if(clientMode=="online")
{
CI=new FChatInfo(S,"stopuser",null,CC,"",null,0,null);
try
{
out.writeObject(CI);
}
catch(IOException e)
{}
setClientModeOff();
}
}
public void send(String us,String com,String cw,String f,Color c,int x,String to)//用户发送消息时调用的方法
{
CI=new FChatInfo(us,com,null,cw,f,c,x,to);
try
{
out.writeObject(CI);
}
catch(IOException e)
{}
}
public void stop(String st)//用户屏蔽时调用的方法
{
StopUser=st;
C.C.B.M.L.U.UserList.removeAll();
C.C.B.M.L.U.UserList.setListData(v);
JLabel W=new JLabel("***用户: "+StopUser+" 已经被屏蔽***");
W.setFont(new Font("宋体",Font.PLAIN,13));
W.setForeground(new Color(164,39,143));
C.C.B.M.T.TA.add(W);
}
public void recieve()//用户恢复屏蔽时调用的方法
{
C.C.B.M.L.U.UserList.removeAll();
C.C.B.M.L.U.UserList.setListData(v);
JLabel W=new JLabel("***用户: "+StopUser+" 的屏蔽被取消了***");
W.setFont(new Font("宋体",Font.PLAIN,13));
W.setForeground(new Color(0,166,18));
C.C.B.M.T.TA.add(W);
StopUser="00000000000000000000000000000";
}
public void leave()//用户暂时离开时调用的方法
{
String S=CCardFrame.hashmapnicheng.get(C.C.A.D.L.TF2.getText()).toString();
if(clientMode=="online")
{
CI=new FChatInfo(S,"leave",null,null,"",null,0,null);
try
{
out.writeObject(CI);
}
catch(IOException e)
{}
}
}
public void Return()//用户回到聊天室时调用的方法
{
String S=CCardFrame.hashmapnicheng.get(C.C.A.D.L.TF2.getText()).toString();
if(clientMode=="online")
{
CI=new FChatInfo(S,"return",null,null,"",null,0,null);
try
{
out.writeObject(CI);
}
catch(IOException e)
{}
}
}
public void run()
{
while(clientMode=="online")
{
try
{
CI=(FChatInfo)in.readObject();
}
catch(IOException e)
{}
catch(ClassNotFoundException e)
{}
if(CI.getCommand().equals("login"))//如果接收到的命令是登录
{
v.add(CI.getUser());
C.C.B.M.L.U.UserList.removeAll();
C.C.B.M.L.U.UserList.setListData(v);
C.C.B.M.L.UserLabel.setText("在线用户数:"+v.size());
JLabel L=new JLabel(CI.getChatText());
L.setFont(new Font("宋体",Font.PLAIN,13));
L.setForeground(new Color(0,128,192));
C.C.B.M.T.TA.add(L);
}
if(CI.getCommand().equals("logout"))//如果接收到的命令是登出
{
v.remove(CI.getUser());
C.C.B.M.L.U.UserList.removeAll();
C.C.B.M.L.U.UserList.setListData(v);
C.C.B.M.L.UserLabel.setText("在线用户数:"+v.size());
JLabel O=new JLabel(CI.getChatText());
O.setFont(new Font("宋体",Font.PLAIN,13));
O.setForeground(new Color(255,0,128));
C.C.B.M.T.TA.add(O);
}
if(CI.getCommand().equals("leave"))//如果接收到的命令是离开
{
V.add(CI.getUser());
C.C.B.M.L.U.UserList.removeAll();
C.C.B.M.L.U.UserList.setListData(v);
JLabel Z=new JLabel(CI.getChatText());
Z.setFont(new Font("宋体",Font.PLAIN,13));
Z.setForeground(new Color(242,109,125));
C.C.B.M.T.TA.add(Z);
}
if(CI.getCommand().equals("return"))//如果接收到的命令是回来
{
V.remove(CI.getUser());
C.C.B.M.L.U.UserList.removeAll();
C.C.B.M.L.U.UserList.setListData(v);
JLabel Z=new JLabel(CI.getChatText());
Z.setFont(new Font("宋体",Font.PLAIN,13));
Z.setForeground(new Color(0,128,255));
C.C.B.M.T.TA.add(Z);
}
if(CI.getCommand().equals("Serverstopuser"))//如果接收到的命令是服务器屏蔽用户
{
v.remove(CI.getUser());
C.C.B.M.L.U.UserList.removeAll();
C.C.B.M.L.U.UserList.setListData(v);
C.C.B.M.L.UserLabel.setText("在线用户数:"+v.size());
JLabel O=new JLabel(CI.getChatText());
O.setFont(new Font("宋体",Font.PLAIN,13));
O.setForeground(new Color(130,130,130));
C.C.B.M.T.TA.add(O);
}
if(CI.getUser().equals(StopUser))//如果接收到的命令是屏蔽
{
continue;
}
if(CI.getCommand().equals("toall"))//如果接收到的命令是群聊
{
String A=CI.getUser()+" 对 "+CI.gettoUser()+" 说: ";
String T=CI.getChatText();
String F=CI.getF();
int X=CI.getx();
JLabel User=new JLabel(A);
JLabel Talk=new JLabel(T,new ImageIcon(X+".gif"),10);
C.C.B.M.T.TA.add(User);
C.C.B.M.T.TA.add(Talk);
User.setFont(new Font("宋体",Font.PLAIN,13));
User.setForeground(new Color(0,0,0));
Talk.setFont(new Font(F,Font.PLAIN,13));
Talk.setForeground(CI.getColor());
}
if(CI.getCommand().equals("toone"))//如果接收到的命令是私聊
{
String A="";
if(CCardFrame.hashmapnicheng.get(C.C.A.D.L.TF2.getText()).equals(CI.getUser()))
{
A="你 悄悄地对 "+CI.gettoUser()+" 说: ";
}
if(CCardFrame.hashmapnicheng.get(C.C.A.D.L.TF2.getText()).equals(CI.gettoUser()))
{
A=CI.getUser()+" 悄悄地对 你 说: ";
}
String T=CI.getChatText();
String F=CI.getF();
int X=CI.getx();
JLabel User=new JLabel(A);
JLabel Talk=new JLabel(T,new ImageIcon(X+".gif"),10);
C.C.B.M.T.TB.add(User);
C.C.B.M.T.TB.add(Talk);
User.setFont(new Font("宋体",Font.PLAIN,13));
User.setForeground(new Color(0,0,0));
Talk.setFont(new Font(F,Font.PLAIN,13));
Talk.setForeground(CI.getColor());
}
if(CI.getCommand().equals("CloseServer"))//如果接收到的命令是关闭服务器
{
setClientModeOff();
JLabel W=new JLabel("***服务器已关闭***");
W.setFont(new Font("宋体",Font.PLAIN,13));
W.setForeground(new Color(255,0,0));
C.C.B.M.T.TA.add(W);
C.C.B.T.State.setForeground(new Color(255,0,0));
C.C.B.T.State.setText("离线");
C.C.B.T.Face.setIcon(new ImageIcon(CCardFrame.hashmapimage.get(C.C.A.D.L.TF2.getText())+"a.jpg"));
}
if(CI.getCommand().equals("StopServer"))//如果接收到的命令是断开服务器
{
setClientModeOff();
JLabel W=new JLabel("***服务器已暂停连接,请稍后登录***");
W.setFont(new Font("宋体",Font.PLAIN,13));
W.setForeground(new Color(255,128,0));
C.C.B.M.T.TA.add(W);
C.C.B.T.State.setForeground(new Color(255,0,0));
C.C.B.T.State.setText("离线");
C.C.B.T.Face.setIcon(new ImageIcon(CCardFrame.hashmapimage.get(C.C.A.D.L.TF2.getText())+"a.jpg"));
}
if(CI.getCommand().equals("StopUser") && CCardFrame.hashmapnicheng.get(C.C.A.D.L.TF2.getText()).equals(CI.getUser()))//如果用户被服务器屏蔽时退出
{
System.out.println("Close Chat!!");
CC=CI.getChatText();
stopuser();
C.dispose();
FChatUser CU=new FChatUser();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -