?? clientreceive.java
字號:
import javax.swing.*;
import java.io.*;
import java.net.*;
/*
* 聊天客戶端消息收發類
*/
public class ClientReceive extends Thread {
private JComboBox combobox;
private JTextArea textarea;
Socket socket;
ObjectOutputStream output;
ObjectInputStream input;
JTextField showStatus;
public ClientReceive(Socket socket,ObjectOutputStream output,
ObjectInputStream input,JComboBox combobox,JTextArea textarea,JTextField showStatus){
this.socket = socket;
this.output = output;
this.input = input;
this.combobox = combobox;
this.textarea = textarea;
this.showStatus = showStatus;
}
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("所有人");
while(i < usernames.length){
combobox.addItem(usernames[i]);
i ++;
}
combobox.setSelectedIndex(0);
showStatus.setText("在線用戶 " + usernames.length + " 人");
}
}
catch (Exception e ){
System.out.println(e);
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -