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

📄 chatappletthree.java

📁 Java 的聊天室的代码 比较简单一点, 适合初学者学习 cs040098 xmu 吴博 厦门大学
💻 JAVA
字号:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//聊天室主类
public class chatappletthree extends Applet 
implements Runnable,ActionListener,ItemListener{
public static final int PORT=1234;//PORT为网络套接字端口号
static String name,xingbie;//name,xingbie分别为聊天人的名字和性别
Socket socket;
int jilu,enter=0;
//jilu为新进入聊天室的人是否与已有聊天室人重名的状态标志
//jilu为0表明无重名可以进入;为1表明有重名,应重新进入
//enter为0,未进入聊天室,发送信息按钮不起作用;enter为1已经进入聊天室
DataInputStream in;//定义读取服务器信息流 in
static DataOutputStream out;//定义写入服务器信息流 out
Thread thread;
String line;//line为读取来自服务器线路的信息
static Mywindow mywindow;
static Apanel a;static Bpanel b;static Cpanel c;
//Applet为驱动初始化画出聊天室界面,建立与服务器连接
//Applet初始化
public void init(){
mywindow=new Mywindow();
setBackground(new Color(113,163,139));
setLayout(new BorderLayout());
a=new Apanel();b=new Bpanel();c=new Cpanel();
add("North",a);add("Center",b);add("South",c);
a.button1.addActionListener(this);
a.button2.addActionListener(this);
c.button.addActionListener(this);
c.button2.addActionListener(this);
c.button3.addActionListener(this);
a.box1.addItemListener(this);
a.box2.addItemListener(this);
a.box3.addItemListener(this);
b.b2.list.addActionListener(this);
add("East",new Label());
add("West",new Label());
jilu=0;
this.setForeground(Color.black);
c.msg_txt.setBackground(Color.white);
b.chat_txt.setBackground(new Color(200,185,200));
b.chat_txt.setFont(new Font("TimeRoman",Font.PLAIN,12));
}
//Applet小程序驱动
public void start(){
//与服务器建立连接
//默认本机运行服务器端程序,与本机IP建立连接
this.getCodeBase().getHost();
try{
socket=new Socket(this.getCodeBase().getHost(),PORT);
in=new DataInputStream(socket.getInputStream());
out=new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){
this.showStatus(e.toString());
say("欢迎来这里!");System.exit(1);
}
say("欢迎来到我的聊天室");
if(thread==null)
{
 thread=new Thread(this);
thread.setPriority(Thread.MIN_PRIORITY);
thread.start();
}
}
//停止小程序运行
public void stop(){
try{
out.writeUTF("QUIT");
}
catch(IOException e){}
}
//结束Applet关闭网络套接字连接,结束用户线程
public void destroy(){
try{
socket.close();}
catch(IOException e){
this.showStatus(e.toString());}
if((thread==null)&&thread.isAlive())
{
thread.yield();
thread=null;
}
}
//-----------------------------------------------------------------------------------------
//定义线程运行操作的方法与服务器通信
public void run()
{ String line;     //通过line读取服务器放入“线路”的信息
  try { while(true)
         {line=in.readUTF();
          //线路信息前端为PEOPLE表明有新人进入了聊天室
           if(line.startsWith("PEOPLE"))
           {  String listString=line;
              if(line.endsWith("*"))
              //去掉名字信息后面的*号
              {listString=line.substring(0,(line.length()-1));}
              b.b2.list.add(listString.substring(6));
              if(!line.endsWith("*"))    //判断是否是来自读取列表的请求
                    { b.chat_txt.append(line.substring(6)+"爬上了红蜘蛛网->"+'\n');}
           }
           //线路信息前端为QUIT表明有人离开了聊天室 
           else if (line.startsWith("QUIT"))
           { //QUIT+PEOPLE为10个字符
              String str=line.substring(10);
              try
              {  for(int i=0,k=0;i<=120;i++)  //聊天室列表中最多存放120个
                  { String s=b.b2.list.getItem(i);  //list列表中存放聊天室人员列表
                    if(s.equals(str))  //判断某人是离开聊天室的人
                     { k=i;b.b2.list.remove(k);  //list列表中清除此人
                       b.chat_txt.append(line.substring(10)+"<-高兴地离开了网络 "+'\n');
                     }//在聊天文本区显示某人离开了聊天室
                  }
              }
              catch(ArrayIndexOutOfBoundsException e) {}
           }
           //线路前端为MSG表明接收到的是普通聊天话语信息
           else if(line.startsWith("MSG"))
           { b.chat_txt.append(line.substring(3)+'\n');}  //在聊天文本区显示话语
           //线路信息前端为“悄悄地对”表明接收到悄悄话
           else if(line.startsWith("悄悄地对"))
           {
             b.chat_txt.append(line+'\n');  //文本区显示悄悄话
           }
         }
      }
  catch(IOException e)
  { say("再见!欢迎再来红蜘蛛聊天室,如果想重新进入本聊天室,单击浏览器的刷新选项"); }
  catch(NullPointerException e) {}
}

//定义聊天室按钮点击事件的处理方法
public void actionPerformed(ActionEvent e)
{
   //点击了进入聊天室按钮Apanel中的a.button1
   if(e.getSource()==a.button1)
   {
     name=new String(a.name_txt.getText());
     try 
     { for(int i=0;i<=120;i++) //判断是否与现有的聊天室人员重名
         { if((a.name_txt.getText()!=null)&&((a.name_txt.getText()+"["+xingbie+"]").equals(b.b2.list.getItem(i))||a.name_txt.getText().equals("该名字已被使用")))
            { jilu=1;name=null;break; }
         }
     }
     catch(ArrayIndexOutOfBoundsException e3) {}
     if(jilu==0)
       { try
            {  out.writeUTF("PEOPLE"+a.name_txt.getText()+"["+xingbie+"]");
               enter=1;
            }
         //向服务器写入进入聊天室信息,enter标志置1,已进入聊天室
         catch(IOException e1){}
       }
      else if(jilu==1)
       {a.name_txt.setText("该名字已被使用");}
      jilu=0;
   }
   //点击了离开聊天室按钮Apanel中的a.button2
   else if(e.getSource()==a.button2)
   { try
           {// 离开聊天室,enter标志置0  
              out.writeUTF("QUIT");enter=0; 
           }
     catch(IOException e1) {}
     b.b2.list.removeAll();//清除聊天人员列表
   }
   //如果是点击了聊天信息发送按钮Cpanle中的c.button
   else if (e.getSource()==c.button&&enter==1)
   {  if(name!=null)
       { try
          { out.writeUTF("MSF"+name+"["+xingbie+"]"+"说->"+":"+c.msg_txt.getText());
            c.msg_txt.setText(null);//发送完毕信息清空写信息文本框
          }
         catch(IOException e1) {}
       }
    }
   //双击了聊天人员列表中某人B2panel中的list弹出窗口发送悄悄话
   else if(e.getSource()==b.b2.list&&enter==1)
    {
       mywindow.setVisible(true);
       mywindow.text1.setText(((List)e.getSource()).getSelectedItem());
    }
   //双击了刷新谈话区按钮Cpanel中的c.button2
   else if(e.getSource()==c.button2)
     { b.chat_txt.setText(null); }//清空聊天文本区
   //点击了刷新聊天列表区按钮Cpanel中的c.button3
   else if(e.getSource()==c.button3)
    {  try
         {b.b2.list.removeAll();out.writeUTF("newlist");}//刷新聊天人员列表
       catch(IOException e1) {}
    }
}
//性别选择框事件的处理
public void itemStateChanged(ItemEvent e1)
{
   if(e1.getItemSelectable()==a.box1)
       {xingbie=new String("男");}
   else if(e1.getItemSelectable()==a.box2)
       {xingbie=new String("女");}
   else if(e1.getItemSelectable()==a.box3)
       {xingbie=new String("蛋蛋");}
}
//各种事件系统消息显示在文本区say方法
public void say(String msg)
{
   b.chat_txt.append("****"+msg+"****\n");
}
}
//-----------------------------------------------------------------------------------------

⌨️ 快捷键说明

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