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

📄 chatarea.java~7~

📁 局域网聊天系统
💻 JAVA~7~
字号:
package com.soft.Team;

import java.awt.*;
import javax.swing.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import java.util.Hashtable;
import java.awt.BorderLayout;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class ChatArea
    extends JFrame implements Readable{
  Socket socket=null;
  DataInputStream in=null;
  DataOutputStream out=null;
  Thread threadMessage=null;
  String name=null;
  Hashtable listTable;
  JPanel contentPane;
  XYLayout xYLayout1 = new XYLayout();
  TextArea 谈话显示区 = new TextArea();
  TextArea 私聊显示区 = new TextArea();
  TextField 送出信息 = new TextField();
  Button 确定 = new Button();
  Button 刷新谈话区 = new Button();
  Button 刷新私聊区 = new Button();
  List listComponent = new List();
  Choice privateChatList = new Choice();
  public ChatArea(String name,Hashtable listTable) {
    try {
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      jbInit(name,listTable);
    }
    catch (Exception exception) {
      exception.printStackTrace();
    }
  }

  /**
   * Component initialization.
   *
   * @throws java.lang.Exception
   */
  private void jbInit(String name,Hashtable listTable) throws Exception {
    this.listTable=listTable;
    this.name=name;
    threadMessage=new Thread(this);
    contentPane = (JPanel) getContentPane();
    contentPane.setLayout(xYLayout1);
    setSize(new Dimension(400, 500));
    setTitle("用户聊天区");
    确定.setLabel("送出信息到:");
    确定.addActionListener(new ChatArea_确定_actionAdapter(this));
    刷新谈话区.setLabel("刷新谈话区");
    刷新谈话区.addActionListener(new ChatArea_刷新谈话区_actionAdapter(this));
    刷新私聊区.setLabel("刷新私聊区");
    刷新私聊区.addActionListener(new ChatArea_刷新私聊区_actionAdapter(this));
    privateChatList.addItemListener(new ChatArea_privateChatList_itemAdapter(this));
    listComponent.addActionListener(new ChatArea_listComponent_actionAdapter(this));
    contentPane.add(谈话显示区, new XYConstraints(16, 18, 262, 144));
    contentPane.add(私聊显示区, new XYConstraints(17, 183, 262, 140));
    contentPane.add(送出信息, new XYConstraints(16, 337, 263, 45));
    contentPane.add(确定, new XYConstraints(16, 402, 167, 29));
    contentPane.add(刷新谈话区, new XYConstraints(16, 438, 168, 28));
    contentPane.add(刷新私聊区, new XYConstraints(190, 438, 141, 28));
    contentPane.add(listComponent, new XYConstraints(293, 21, 76, 361));
    contentPane.add(privateChatList, new XYConstraints(191, 403, 142, 26));
    privateChatList=new Choice();
    privateChatList.add("大家(*)");
    privateChatList.select(0);

  }
  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 run(){

    while(true)
       {
          String s=null;
           try
              {
                s=in.readUTF();
                if(s.startsWith("聊天内容:"))
                   {
                     String content=s.substring(s.indexOf(":")+1);
                     谈话显示区.append("\n"+content);
                   }
                if(s.startsWith("私人聊天内容:"))
                   {
                     String content=s.substring(s.indexOf(":")+1);
                     私聊显示区.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();
                     谈话显示区.append("\n"+(String)listTable.get(awayPeopleName)+"离线");
                     listTable.remove(awayPeopleName);
                   }
                Thread.sleep(5);
              }
           catch(IOException e)
              {
                 listComponent.removeAll();
                 listComponent.repaint();
                 listTable.clear();
                 谈话显示区.setText("和服务器的连接已中断\n必须刷新浏览器才能再次聊天");
                 break;
              }
           catch(InterruptedException e)
              {
              }
       }

  }

  public void 确定_actionPerformed(ActionEvent e) {
    String message="";
      String people=privateChatList.getSelectedItem();
      people=people.substring(0,people.indexOf("("));
      message=送出信息.getText();
      if(message.length()>0)
      {
       try {
             if(people.equals("大家"))
               {
                 out.writeUTF("公共聊天内容:"+name+"说:"+message);
               }
             else
               {
                 out.writeUTF("私人聊天内容:"+name+"悄悄地说:"+message+"#"+people);
               }
           }
       catch(IOException event)
           {
           }
      }

  }

  public void 刷新谈话区_actionPerformed(ActionEvent e) {
       谈话显示区.setText(null);
  }

  public void 刷新私聊区_actionPerformed(ActionEvent e) {
      私聊显示区.setText(null);
  }

  public void privateChatList_itemStateChanged(ItemEvent e) {

  }

  public void listComponent_actionPerformed(ActionEvent e) {
    privateChatList.insert(listComponent.getSelectedItem(),0);
    privateChatList.repaint();
  }
}

class ChatArea_privateChatList_itemAdapter
    implements ItemListener {
  private ChatArea adaptee;
  ChatArea_privateChatList_itemAdapter(ChatArea adaptee) {
    this.adaptee = adaptee;
  }

  public void itemStateChanged(ItemEvent e) {
    adaptee.privateChatList_itemStateChanged(e);
  }
}

class ChatArea_listComponent_actionAdapter
    implements ActionListener {
  private ChatArea adaptee;
  ChatArea_listComponent_actionAdapter(ChatArea adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.listComponent_actionPerformed(e);
  }
}

class ChatArea_刷新私聊区_actionAdapter
    implements ActionListener {
  private ChatArea adaptee;
  ChatArea_刷新私聊区_actionAdapter(ChatArea adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.刷新私聊区_actionPerformed(e);
  }
}

class ChatArea_刷新谈话区_actionAdapter
    implements ActionListener {
  private ChatArea adaptee;
  ChatArea_刷新谈话区_actionAdapter(ChatArea adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.刷新谈话区_actionPerformed(e);
  }
}

class ChatArea_确定_actionAdapter
    implements ActionListener {
  private ChatArea adaptee;
  ChatArea_确定_actionAdapter(ChatArea adaptee) {
    this.adaptee = adaptee;
  }

  public void actionPerformed(ActionEvent e) {
    adaptee.确定_actionPerformed(e);
  }
}

⌨️ 快捷键说明

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