⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatarea.java

📁 java网络聊天CS模式
💻 JAVA
字号:
/*
 * Title:        网络应用
 * Description:  b/s模式网络聊天室
 * Copyright:    Copyright (c) 2004  飞鱼工作室
 * Company:      HOHAI
 * @author: lishaofeng,yuanfeng
 * @version: 1.0
 */
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;
import javax.swing.*;

public class ChatArea extends Panel implements ActionListener
{
InputNameTextField UserHdChat=null;
  ClientChat client;
  //ClientChat chat;
  Socket  socket=null;      //和服务器间建立的login的套接字.                         
  DataInputStream in=null;       //读取服务器信息的输入流.                    
  DataOutputStream out=null;      //向服务器发送信息的输出流.                   
  Thread threadMessage=null;      //读取服务器信息的线程.                   
  TextArea PubArea,PriArea=null;
  TextField sendmsg=null;
  Button OK,RefPubArea,RefPriArea;
  Label  Inf=null;
  String name=null;        //聊天者的昵称.                          
  Hashtable listTable;         //存放在线聊天者昵称的散列表.                      
  List  listComponent=null;        //显示在线聊天者昵称的List组件.                  
  Choice privateChatList;          //选择私人聊天者的下拉列表.                   
  int width,height;                //聊天区的宽和高.
//    	InputNameTextField UserHdChat=null;                   
  public ChatArea(String name,Hashtable listTable,int width,int height,ClientChat client)
  {
    setLayout(null); 
    setBackground(Color.orange);
    this.width=width;
    this.height=height;
    setSize(width,height);
    this.listTable=listTable;
    this.name=name;
    this.client=client;
//    threadMessage=new Thread(this); 
    PubArea=new TextArea(10,10);
    PriArea=new TextArea(10,10);
    OK=new Button("发送消息到:");
    RefPubArea=new Button("刷新公共显示区");
    RefPriArea=new Button("刷新私聊显示区");
    Inf=new Label("双击聊天者可私聊",Label.CENTER);
    sendmsg=new TextField(50);
    OK.addActionListener(this);
    sendmsg.addActionListener(this);
    RefPubArea.addActionListener(this);
    RefPriArea.addActionListener(this);
    listComponent=new List();
    listComponent.addActionListener(this);         //双击列表中的聊天者昵称,可选中某聊天者与之私聊。  
    privateChatList=new Choice();
    privateChatList.add("大家(*)");
    privateChatList.select(0);                      //默认情况下,用户输入内容发送给所有聊天者。   
    add(PubArea);
    PubArea.setBounds(10,10,(width-120),(height-120)/2); 
    add(PriArea);
    PriArea.setBounds(10,10+(height-120)/2,(width-120),(height-120)/2);
    add(listComponent);
    listComponent.setBounds(10+(width-120),10,100,(height-160));
    add(Inf);
    Inf.setBounds(10+(width-120),10+(height-160),110,40);
    Panel  pSouth=new Panel();
    pSouth.add(sendmsg);
    pSouth.add(OK);
    pSouth.add(privateChatList);
    pSouth.add(RefPubArea);
    pSouth.add(RefPriArea); 
    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;
//    if(UserHdChat.getCanChat==true)
    try             //启动线程,接收服务器信息.
         { 
           threadMessage.start();
         }
    catch(Exception e)
         {
         } 
  }
  	public void ProcessMessage(String s)
	{
//		System.out.println("收到消息");
		if(s.startsWith("聊天内容:"))   //读取服务器发来的信息.
        {
           String content=s.substring(s.indexOf(":")+1);
           PubArea.append("\n"+content);
        }
        if(s.startsWith("私人聊天内容:"))   //读取服务器发来的信息.
        {
            String content=s.substring(s.indexOf(":")+1);
            PriArea.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+")");    //再将昵称添加到List组件中:
                     
             listComponent.add((String)listTable.get(people)); 
             listComponent.repaint();  //刷新List组件,显示新用户昵称.                     
         } 
         else if(s.startsWith("用户离线:"))
         {             //删除已离线的聊天者信息.
              String awayPeopleName=s.substring(s.indexOf(":")+1);
              listComponent.remove((String)listTable.get(awayPeopleName));
              listComponent.repaint();
              PubArea.append("\n"+(String)listTable.get(awayPeopleName)+"离线"); 
              listTable.remove(awayPeopleName);
         }		
	}
 public void actionPerformed(ActionEvent e)
  {
  	
//   System.out.println("faba");
   if(e.getSource()==OK||e.getSource()==sendmsg)
     {  
        String message="";
        String people=privateChatList.getSelectedItem();
        people=people.substring(0,people.indexOf("("));     //获取信息发送对象的昵称.
        message=sendmsg.getText();
        if(message.length()>0)
        {  
                   //将聊天内容及对象发送给服务器
            name=client.name;
//           System.out.println(name);
           String x;
         try {
               if(people.equals("大家"))
                 {
                 	 
                   out.writeUTF("公共聊天内容:"+name+"说:"+message);
                 }
               else
                 {
                 	x="你对"+people+"悄悄地说:"+message;
                   out.writeUTF("私人聊天内容:"+name+"对你悄悄地说:"+message+"#"+people);
                   PriArea.setText(x);
                 } 
                 
             }
         catch(IOException event)
             {
             }
        }
     }
    else if(e.getSource()==listComponent)
     {
       privateChatList.insert(listComponent.getSelectedItem(),0);
       privateChatList.repaint();
     }
    else if(e.getSource()==RefPubArea)
     {
       PubArea.setText(null);
     }
    else if(e.getSource()==RefPriArea)
     {
       PriArea.setText(null);
     }
   }	  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -