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