ChatArea.java

原创 2007年10月05日 22:46:00
package chatroom;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;
public class ChatArea extends Panel implements ActionListener,Runnable{
 Socket socket=null;
 DataInputStream in=null;
 DataOutputStream out=null;
 Thread threadMessage=null;
 TextArea publicChat,privateChat;
 TextField msg=null;
 Button b1,b2,b3;
 Label tishi=null;
 String name=null;
 Hashtable listTable;
 List listComponent=null;
 Choice privateChatList;
 int width,height;
 
 public ChatArea (String name,Hashtable listTable,int width,int height){
  setLayout(null);
  setBackground(Color.orange);
  this.width=width;
  this.height=height;
  setSize(width,height);
  this.listTable=listTable;
  this.name=name;
  threadMessage=new Thread(this);//用来监听来自服务器端的信息
  publicChat=new TextArea(10,10);
  privateChat=new TextArea(10,10);
  b1=new Button("送出信息到:");
  b2=new Button("刷新谈话区:");
  b3=new Button("刷新私聊区: ");
  tishi=new Label("双击可以私聊",Label.CENTER);
  msg=new TextField(28);
     b1.addActionListener(this);
     msg.addActionListener(this);
     b2.addActionListener(this);
     b3.addActionListener(this);
     listComponent=new List();
     listComponent.addActionListener(this);//双击可以私聊
    
     privateChatList=new Choice();
     privateChatList.add("大家(*)");
     privateChatList.select(0);
 
     add(publicChat);
     publicChat.setBounds(10,10,(width-120)/2,(height-120));
     add(privateChat);
     privateChat.setBounds(10+(width-120)/2,10,(width-120)/2,(height-120));
     add(listComponent);
     listComponent.setBounds(10+(width-120),10,100,(height-160));
     add(tishi);
     tishi.setBounds(10+(width-120),10+(height-160),110,40);
    
     Panel pSouth=new Panel();
     pSouth.add(msg);
     pSouth.add(b1);
     pSouth.add(privateChatList);
     pSouth.add(b2);
     pSouth.add(b3);
     add(pSouth);
     pSouth.setBounds(10,20+(height-120),width-20,60);
 }
 
 public void setName(String s){
  name=s;
 }
 
 public void setSocketConnection(Socket socket,DataInputStream in,DataOutputStream out){
  this.socket=socket;
  this.in=in;
  this.out=out;
  try{
   threadMessage.start();
  }catch(Exception e){
  }
 }
 
 public void actionPerformed(ActionEvent e){
  System.out.print("hahahaha1");
  if(e.getSource()==b1||e.getSource()==msg){
   String message="";
   String people=privateChatList.getSelectedItem();
   people=people.substring(0,people.indexOf("("));
   message=msg.getText();
   if(message.length()>0){
    try{
     if(people.equals("大家"))
               out.writeUTF("公共聊天内容:"+name+"说:"+message);
              else
               out.writeUTF("私人聊天内容:"+name+"悄悄地说:"+message+"#"+people);
    }catch(IOException ee){
    }
   }
  }
  else if(e.getSource()==listComponent){//处理双击事件
   privateChatList.insert(listComponent.getSelectedItem(), 0);
   privateChatList.repaint();
  }else if(e.getSource()==b2){
   publicChat.setText(null);}
  else if(e.getSource()==b3){
   privateChat.setText(null);}
 }
   
 
 
 public void run(){
  while(true){
   String s=null;
   try{
    s=in.readUTF();
    if(s.startsWith("公共聊天内容:")){
     String content=s.substring(s.indexOf(":")+1);
     publicChat.append("/n"+content);
    }
    if(s.startsWith("私人聊天内容:")){
     String content=s.substring(s.indexOf(":")+1);
     privateChat.append("/n"+content);
    }else if(s.startsWith("聊天者:")){
     String people=s.substring(s.indexOf(":")+1,s.indexOf("性别"));
     String sex=s.substring(s.indexOf("性别")+2);
     listTable.put(people, people+"("+sex+")");
     listComponent.add((String)listTable.get(people));
     listComponent.repaint();
    }else if(s.startsWith("用户离线:")){
     String awayPeopleName=s.substring(s.indexOf(":")+1);
     listComponent.remove((String)listTable.get(awayPeopleName));
     listComponent.repaint();
     publicChat.append("/n"+(String)listTable.get(awayPeopleName)+"");
     listTable.remove(awayPeopleName);
    }
    Thread.sleep(5);
   }catch(IOException e){
    listComponent.removeAll();
    listComponent.repaint();
    listTable.clear();
    publicChat.setText("和服务器的连接已经断开/n必须刷新浏览器才能再次进入聊天室。");
    break;
   }catch(InterruptedException e){}
  }
  
 }
}
 
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

jd-gui java 反编译

Java基础面试题

java 设计模式 其一 单例模式

单例模式转载出处(侵删) http://www.blogjava.net/kenzhh/archive/2013/03/15/357824.html 有的时候,我们在全局中,为了不制造多个实例,避...

java20套项目

java入门常识——第一天

此笔记是本人在0基础学习过程中整理的笔记,这是第一天

java思维导图

java线程池简介

Java通过Executors提供四种线程池,分别为: newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。 new...

java编程思想chm

内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)