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

📄 chatappletthree.java

📁 shi yi ge liao tian chengxu
💻 JAVA
字号:


import  java.net.*;
import  java.io.*;
import  java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*    发悄悄话的弹出式窗口类Mywindow    */
class Mywindow extends Frame implements ActionListener
 {     TextField text1,text2;     Button button1,button2;
   //构造函数初始化画出弹出式窗口界面
  Mywindow()
   {  super("发悄悄话窗口");
       setLayout (new GridLayout(3,2));
       text1=new TextField(12);        text2=new  TextField(8);
       button1=new Button("送出悄悄话");     button2=new Button("关闭此窗口");
       add(new Label("送悄悄话到:"));   add(text1);  add(new Label("输入您的悄悄话:"));
       add(text2);   add(button2);  add(button1);
       setSize(400,190);  text1.setEditable(false);   setVisible(false);
       button1.addActionListener(this);     button2.addActionListener(this);
       setBackground(Color.pink);
       addWindowListener(   new WindowAdapter()  //使窗口右上角关闭按钮可以用
                  {    public  void windowClosing(WindowEvent e)   
                           {  setVisible(false);  System.exit(0);   }
                   }
                                              );
    }
    //处理按钮事件的方法
    public void actionPerformed(ActionEvent  e)   
     {  if(e.getSource()==button1)          //向服务器发送悄悄话
        {   
        	 try     {  chatappletthree.out.writeUTF("悄悄地对"+text1.getText()+
                                  "说:"+text2.getText() +"(我是"+chatappletthree.name+")");  
                           
                         }
              catch(IOException e1 )    {     }
         }
         else if(e.getSource()==button2)   //关闭窗口
           {   this.setVisible(false);     }
     }                                   	
 }
 
 //Apanel,Bpanel,B2panel,Cpanel4个面板类画出聊天室主界面
 
 /*  聊天室界面北部面板Apanel类       */
class Apanel  extends Panel
  {   TextField  name_txt;    Button  button1,button2;
       Checkbox  box1,box2,box3;   CheckboxGroup  sex;     
       Apanel()
     {  name_txt=new TextField(10);  
         button1=new Button("进入聊天室");	
         button2=new Button("退出聊天室");
         setLayout(new FlowLayout());     sex=new CheckboxGroup();
         box1=new Checkbox("男M",false,sex);
         box2=new Checkbox("女F",false,sex);
         box3=new Checkbox("蘑菇",true,sex);
         add(new Label("输入昵称"));    
         add(name_txt);   add(box1);     add(box2);    add(box3);
         add(button1);        add(button2);
         add(new Label(""));
     }  	
  }
 
 /*   聊天室界面中部面板Bpanel类   */
class Bpanel  extends  Panel
  {   TextArea  chat_txt;   B2panel  b2;    //chat_txt即聊天文本显示区20行75列
       Bpanel()
      {   chat_txt=new TextArea(25,75);
          b2=new  B2panel();
          chat_txt.setEditable(false);
          setLayout(new FlowLayout());
          add(chat_txt);     add(b2);
      } 	
  } 
  /*   聊天室界面中部面板B2panel类   */ 
 class B2panel  extends Panel
  {   java.awt.List   list; 
      B2panel()
      {  try  {  list=new java.awt.List(25,false);    }
         catch(NullPointerException e)    {   }
         setLayout(new BorderLayout());
         add("Center",list);     add("North",new Label("聊天者列表:"));
         add("East",new  Label());     	
         add("South",new Label("双击某昵称可悄悄话"));
       }
    }
 /*   聊天室界面南部面板Cpanel类   */    
  class Cpanel  extends  Panel
   {  TextField  msg_txt;   Button  button,button2,button3;  //msg_txt为聊天文本框输入区
       Cpanel()
       {  msg_txt=new TextField(44);    button=new   Button("送出");
          button2=new  Button("刷新谈话区");
          button3=new  Button("刷新聊天者列表");
          setLayout(new  FlowLayout());
          add(new Label("您要说的话:"));add(msg_txt);
          add(button);  add(button2);   add(button3);
       }   
    }
       
/*  聊天室主类chatappletthree类 */    
    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表明无重名可以进入;jilu=1  表明有重名,应重新输入
                                                                       //enter为进入聊天室的标志,enter=1  已经进入聊天室
                                                                       //enter=0   未进入聊天室,发送信息按钮不起作用
        DataInputStream in;         //定义读取服务器信息流 in               
        static DataOutputStream  out;  //定义写入服务器信息流 out
        Thread thread;    String  line;     //line读取来自服务器线路的信息
        static Apanel a;  static Bpanel  b;  static  Cpanel c;
        static  Mywindow  mywindow;       //mywindow为悄悄话窗口Mywindow类对象
        
        //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,220));
           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"))
                           {  String  str=line.substring(10);  //QUIT+PEOPLE为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;    }
                   catch(IOException  e1)   {  }   //向服务器写入进入聊天室信息,enter标志置1,已进入聊天室
                }
              else if(jilu==1)
               {  a.name_txt.setText("该名已被使用");  }
               jilu=0;
           }
     
         
          //点击了离开聊天室按钮   Apanel中的a.button2 
            else if(e.getSource()==a.button2)
              {  try  
                   {  out.writeUTF("QUIT");    enter=0;    }   //离开聊天室,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("MSG"+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 + -