📄 clientreceivethread.java
字号:
/*
* Created on 2005-12-19
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package chatClient;
import javax.swing.*;
import java.io.*;
import java.net.*;
/**
* @author hongyuan
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ClientReceiveThread extends Thread {
private JComboBox comboBox;
private JTextArea textArea;
Socket socket;//连接服务器的套接字
ObjectOutputStream output;
ObjectInputStream input;
public ClientReceiveThread(
Socket socket,
ObjectOutputStream output,
ObjectInputStream input,
JComboBox comboBox,
JTextArea textArea
)
{
this.socket=socket;
this.output=output;
this.input=input;
this.comboBox=comboBox;
this.textArea=textArea;
}
public void run()
{
//若套接字没有关闭,则一直执行循环体
while(!socket.isClosed())
{
try
{
//读取服务器聊天信息
String type=(String)input.readObject();
if(type.equalsIgnoreCase("系统信息"))//处理系统信息
{
String sysmsg=(String)input.readObject();
textArea.append("系统信息:"+sysmsg);//显示信息
}
else if(type.equalsIgnoreCase("服务关闭"))
{
output.close();
input.close();
socket.close();
textArea.append("服务器已关闭!\n");
break;
}
else if(type.equalsIgnoreCase("聊天信息"))
{
String message=(String)input.readObject();//接收聊天信息内容
textArea.append(message);
}
else if(type.equalsIgnoreCase("用户列表"))
{
String userlist=(String)input.readObject();//接收用户列表内容
//分析列表内容,并将更新界面用户列表组合框
String usernames[]=userlist.split("\n");
comboBox.removeAllItems();
int i=0;
comboBox.addItem("all");
while(i<usernames.length)
{
comboBox.addItem(usernames[i]);
i++;
}
comboBox.setSelectedIndex(0);
}
}catch(Exception e){}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -