黑马程序员技术交流社区
标题:
聊天系统,客户端Client(四)
[打印本页]
作者:
王鑫宇
时间:
2011-8-4 18:13
标题:
聊天系统,客户端Client(四)
[code=java]//接上
public void setVisible(String friendName){
int num = JOptionPane.showConfirmDialog(this, "确定要设置对好友 "+friendName+" 隐身可见吗?","好友设置",0);
if(num!=0) return;
write("k"+friendName);
}
public void setDisvisible(String friendName){
int num = JOptionPane.showConfirmDialog(this, "确定要取消对好友 "+friendName+" 隐身可见吗?","好友设置",0);
if(num!=0) return;
write("d"+friendName);
}
public void setStopful(String friendName){
int num = JOptionPane.showConfirmDialog(this, "确定要屏蔽好友 "+friendName+" 吗?","好友设置",0);
if(num!=0) return;
stopFriend.add(friendName);
write("b"+friendName);
}
public void setStopless(String friendName){
stopFriend.remove(friendName);
int num = JOptionPane.showConfirmDialog(this, "确定要解除对好友 "+friendName+" 吗?","好友设置",0);
if(num!=0) return;
write("v"+friendName);
}
public void quit(){
new ClientInterface(userName,password);
this.dispose();
}
public boolean showMyInfo(){
if(csmi == null) {
csmi = new ClientShowMyInfo();
return true;
}
return false;
}
public boolean showChangePassword(){
if(ccp == null) {
ccp = new ClientChangePassword();
return true;
}
return false;
}
public boolean showCheckRecall(){
if(ccr == null) {
ccr = new ClientChangePassword();
return true;
}
return false;
}
public boolean showAddFriend(){
if(caf == null){
caf = new ClientAddFriend();
return true;
}
return false;
}
public boolean showAddGroup(){
if(cag == null){
cag = new ClientAddGroup();
return true;
}
return false;
}
public boolean showCheckFriend(String name,boolean b){
if(csfi==null){
csfi = new ClientShowFriendInfo(name,b);
try {
dos.writeUTF("s"+name);
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
return false;
}
public boolean showCheckGroup(String name,boolean b){
if(csgi==null){
csgi = new ClientShowGroupInfo(name,b);
try {
dos.writeUTF("q"+name);
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
return false;
}
public boolean showStopInfo(){
return false;
}
public boolean showChatGroup(){
if(ccg == null) {
ccg = new ClientChatGroup(this);
return true;
}
return false;
}
public void setinfo(int x,int y){
System.out.println(x);
System.out.println(y);
// jpm = new JPopupMenu();
// jpm.setBounds(0,475,100,50);
// jpm.add(menu1_1);
// this.add(jpm);
}
public void setOnLine(String name){
for(int i = 0 ; i <allFriend.size();i++){
if(allFriend.get(i).get("friendName").equals(name)&&!allFriend.get(i).get("state").equals("在线")){
allFriend.get(i).put("state", "在线");
String info = model.getElementAt(i).toString();
info = info.substring(0,info.lastIndexOf("<"))+"<在线>"+info.substring(info.lastIndexOf(">")+1);
model.set(i, info);
memberList.setModel(model);
onNum++;
currentNumber.setText("当前在线人数:"+onNum);
currentNumber.validate();
memberList.validate();
}
}
}
public void setOutLne(String name){
for(int i = 0 ; i <allFriend.size();i++){
if(allFriend.get(i).get("friendName").equals(name)&&!allFriend.get(i).get("state").equals("离线")){
allFriend.get(i).put("state","离线");
String info = model.getElementAt(i).toString();
info = info.substring(0,info.lastIndexOf("<"))+"<离线>";
model.set(i,info);
memberList.setModel(model);
onNum--;
currentNumber.setText("当前在线人数: "+onNum);
currentNumber.validate();
memberList.validate();
}
}
}
public void alert(String msg){
JOptionPane.showMessageDialog(this, msg);
}
public void send(String msg){
try {
dos.writeUTF(msg);
} catch (IOException e) {
e.printStackTrace();
}
}
public void doGet(String msg){
String friendName = msg.substring(0,msg.indexOf("("));
for(int n = 0 ; n < stopFriend.size();n++){
if(stopFriend.get(n).toString().equals(friendName)){
return;
}
}
String message = msg.substring(msg.indexOf(")")+1);
if(chat.containsKey(friendName)){
chat.get(friendName).doGet(message);
}
else{
for(int i = 0 ; i < allFriend.size();i++){
if(allFriend.get(i).get("friendName").equals(friendName)){
ClientChatWindow c = new ClientChatWindow(friendName,allFriend.get(i).get("nickName").toString());
chat.put(friendName, c);
c.doGet(message);
}
}
}
}
public void run() {
try {
while(true){
String msg=dis.readUTF();
if(msg!=null){
if(msg.equals("q")) {
System.exit(1);
break;
}
if(msg.charAt(0)=='g'){
if(csmi!=null){
if(msg.substring(1).equals("1"))csmi.msg("OK");
else csmi.msg("NO");
}
continue;
}
if(msg.charAt(0)=='o'){
model.addElement(msg.substring(1));
memberList.setModel(model);
memberList.validate();
userCount=model.getSize();
this.currentNumber.setText("当前在线人数:"+userCount);
continue;
}
if(msg.charAt(0)=='u'){
allUserName.add(msg.substring(1));
continue;
}
if(msg.charAt(0)=='s'){
friendSecondName = msg.substring(1);
continue;
}
if(msg.charAt(0)=='0'){
if(ccg!=null)
ClientChatGroup.add("0"+"[系统消息:] 欢迎 "+friendSecondName+" 登录聊天室!\n");
continue;
}
else if(msg.charAt(0)=='2'){
if(ccg==null)
showChatGroup();
ClientChatGroup.add(msg);
continue;
}
else if(msg.charAt(0)=='3'){
if(ccg==null)
showChatGroup();
ClientChatGroup.add(msg);
continue;
}
else if(msg.charAt(0)=='m'){
if(csmi==null) continue;
csmi.setInfo(msg.substring(1));
continue;
}
else if(msg.charAt(0)=='f'&&!msg.substring(0,6).equals("friend")){
csfi.setInfo(msg.substring(1));
continue;
}
else if(msg.charAt(0)=='a'){
if(csfi!=null)
csfi.alert(msg.substring(1));
continue;
}
else if(msg.charAt(0)=='l'){
String friendName = msg.substring(1);
setOnLine(friendName);
continue;
}
else if(msg.charAt(0)=='r'){
write("EXIT");
alert("有人登录此账号");
}
else if(msg.length()>6&&msg.substring(0, 6).equals("friend")){
f = msg;
HashMap hashMap = allFriendAddFriend();
model.addElement(hashMap.get("nickName")+"("+hashMap.get("friendName")+") <"+hashMap.get("state")+">");
if(hashMap.get("state").toString().equals("在线")){
onNum++;
currentNumber.setText("当前在线人数:"+onNum);
}
memberList.setModel(model);
memberList.validate();
currentNumber.validate();
continue;
}
else if(msg.charAt(0)=='t'){
setOutLne(msg.substring(1));
continue;
}
else if(msg.charAt(0)=='p'){
int num = Integer.parseInt(msg.substring(1,msg.indexOf(";")));
String friendName = msg.substring(String.valueOf(num).length()+2,num+String.valueOf(num).length()+2);
String nickName = msg.substring(num+String.valueOf(num).length()+2);
HashMap<String,String> hashMap = new HashMap<String, String>();
hashMap.put("friendName",friendName );
hashMap.put("nickName", nickName);
hashMap.put("state", "在线");
allFriend.add(hashMap);
model.addElement(nickName+"("+friendName+") <在线>");
onNum++;
currentNumber.setText("当前在线人数:"+onNum);
memberList.setModel(model);
memberList.validate();
currentNumber.validate();
alert("用户 "+friendName+" 添加您为好友");
continue;
}
else if(msg.charAt(0)=='c'){
if(ccp!=null){
if(msg.substring(1).equals("0"))ccp.alert("修改失败");
ccp.alert("修改成功");
}
continue;
}
else if(msg.charAt(0)=='5'){
doGet(msg.substring(1));
continue;
}
else if(msg.charAt(0)=='k'){
if(msg.substring(1).equals("1")) alert("对好友隐身可见设置成功!");
else{ alert("对好友隐身可见设置失败!");}
continue;
}
else if(msg.charAt(0)=='d'){
if(msg.substring(1).equals("1")) alert("取消对好友隐身可见设置成功!");
else{ alert("取消对好友隐身可见设置失败!");}
continue;
}
else if(msg.charAt(0)=='b'){
if(msg.substring(1).equals("1")) {
alert("对好友屏蔽设置成功!");
model.set(memberList.getSelectedIndex(),memberList.getSelectedValue()+"(已屏蔽)");
memberList.setModel(model);
}
else{ alert("对好友屏蔽设置失败!");}
continue;
}
else if(msg.charAt(0)=='v'){
if(msg.substring(1).equals("1")) {
alert("取消对好友屏蔽设置成功!");
model.set(memberList.getSelectedIndex(),memberList.getSelectedValue().toString().substring(0, memberList.getSelectedValue().toString().lastIndexOf("(")));
memberList.setModel(model);
}
else{ alert("取消对好友屏蔽设置失败!");}
continue;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}[/code]
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2