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

📄 chatfram.java

📁 上传的聊天系统程序与QQ程序差不多,只是功能没有QQ那么强大
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import java.awt.List;
import java.net.*;
import javax.swing.Icon;

public class chatfram implements Runnable
{
  /*以下用于定义UI变量*/
   JFrame frame = new JFrame();
   
   JPanel contentPane;
   JPanel listmenu;
   List friendlist=new List();
   List strangerlist=new List();
   chatarea login[]=new chatarea[10];
   Thread client=new Thread(this);             //定义一个客户端线程 
   Socket soc=null;
   int i=0,count=0;
   String membernum[]=new String[10];
   
   JButton goodfriend,stranger;
   JLabel clientnm;
   
   Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); //获得屏幕尺寸
   String clientname;
   DataInputStream dis=null;          //定义用来实现客户端接受服务器数据的输入流
   DataOutputStream dos=null;          //定义用来实现从客户端发送数据到服务器的输出流
   ImageIcon im[]=new ImageIcon[10];
   JLabel imgLabel;
   
   public chatfram(String s,DataInputStream i ,DataOutputStream o)
   { 
      for(int m=0;m<10;m++)
        im[m]= new ImageIcon(String.valueOf(m+1)+".jpg");
      
  	  ButtonAction buttonaction = new ButtonAction();
  	  clientname=s;
  	  dis=i;
  	  dos=o;
  	  frame.setTitle("PP2007");
  	  contentPane = (JPanel)frame.getContentPane();
  	  contentPane.setLayout(null);
  	  contentPane.setBackground(new Color(111,202,255));
  	  
  	  imgLabel = new JLabel(im[Integer.parseInt(s)-1]);
  	  imgLabel.setBounds(10,10,im[Integer.parseInt(s)-1].getIconWidth(), im[Integer.parseInt(s)-1].getIconHeight());
  	  contentPane.add(imgLabel);

  	  clientnm=new JLabel();
   	  clientnm.setText(s+"(在线)"); 
   	  clientnm.setBounds(60,15,100,25);
   	  contentPane.add(clientnm);
   	  
  	  listmenu=new JPanel();
  	  listmenu.setLayout(null);
  	  listmenu.setBackground(Color.white);
  	  listmenu.setBounds(2,60,190,700);
  	  contentPane.add(listmenu);
  	  
  	  goodfriend=new JButton();
  	  goodfriend.setBackground(new Color(109,190,255));
  	  goodfriend.setBounds(1,1,188,25);
  	  goodfriend.setText("我的好友");
  	  goodfriend.addActionListener(buttonaction);
  	  listmenu.add(goodfriend);
  	  
  	  stranger=new JButton();
  	  stranger.setBackground(new Color(109,190,255));
  	  stranger.setBounds(1,674,188,25);
  	  stranger.setText("陌生人");
  	  stranger.addActionListener(buttonaction);
  	  listmenu.add(stranger);
  	  
  	  friendlist=new List();
  	  friendlist.setBounds(1,26,188,647);
  	  friendlist.setBackground(Color.white);
  	  friendlist.addActionListener(buttonaction);
  	  listmenu.add(friendlist);
  	  friendlist.setVisible(true);
  	  
  	  strangerlist=new List();
  	  strangerlist.setBackground(Color.white);
  	  strangerlist.setBounds(1,52,188,647);
  	  strangerlist.addActionListener(buttonaction);
  	  listmenu.add(strangerlist);
  	  strangerlist.setVisible(false);
  	  
  	  frame.setSize(200,800);
  	  frame.addWindowListener(new WindowAdapter()
      {  
         public void windowClosing(WindowEvent e)
         {  
            disconnect();
            System.exit(0);
         }
      });
  	  frame.setResizable(false);
  	  frame.setLocation((screenSize.width-204),0);
  	  frame.setVisible(true);
  	  client.start();
   }
   

   class ButtonAction implements ActionListener
   {
   public void actionPerformed(ActionEvent event)
   {  
	   Object source=event.getSource();
	   if(source==goodfriend)
	   {
	      stranger.setBounds(1,674,188,25);
	      friendlist.setVisible(true);
	      strangerlist.setVisible(false);
	   }
	   else if(source==stranger)
	   {
	      stranger.setBounds(1,27,188,25);
	      friendlist.setVisible(false);
	      strangerlist.setVisible(true);
	   }
	   else if(source==friendlist)
	   {
	      
	      String []shortstring=friendlist.getSelectedItem().split("(");
	      boolean at=true; 
	      if(shortstring[shortstring.length-1].equals("在线)"))
	      {
		      String reciver=friendlist.getSelectedItem();
		      StringTokenizer st=new StringTokenizer(reciver,"(");
		      String petname=null;
		      reciver=st.nextToken();
		      for(int j=0;j<count;j++)
		      {
		      	 if(membernum[j].equals(clientname))
		      	  {   
		      	      petname=friendlist.getItem(j);
		      	      break;
		      	  }
		      }
	          st=new StringTokenizer(petname,"(");
	          petname=st.nextToken();	       
		      if(!clientname.equals(membernum[friendlist.getSelectedIndex()]))
		      {
		      	 if(i!=0)
		      	 {
		      	 	for(int k=0;k<i;k++)
		      	    {
		      	 	   System.out.println(membernum[friendlist.getSelectedIndex()]+login[k].reciver);
		      	 	   if(membernum[friendlist.getSelectedIndex()].equals(login[k].reciver))
		      	 	   {  
		      	 	      login[k].frame.setVisible(true);
		      	 	      at=false;
		      	 	      break;
		      	 	   }
		            }
		         }
		         else if(at=true)
		         {
		         	login[i]=new chatarea(membernum[friendlist.getSelectedIndex()],clientname,reciver,petname,dis,dos);
		            login[i++].frame.setVisible(true);
		         }
		      } 
		      else
		      {
		      	 JFrame ff=new JFrame();
		      	 JOptionPane.showMessageDialog(ff,"不能给自己发信息","警告",JOptionPane.WARNING_MESSAGE);
		      } 	 
	      }
	      else
	      {
	          JFrame ff=new JFrame();
		      JOptionPane.showMessageDialog(ff,"对方不在线,不可发送信息!","警告",JOptionPane.WARNING_MESSAGE);	  
	      }
	   }
	}
	}
	
   public void run()                               //线程运行方法
   {
     
      String msg=null;
	  while(true)
	  {
	     try
	     {
	     	 msg=dis.readUTF();
	     	 System.out.println(msg);
	     }                 //读取从服务器传来的信息
		 catch(IOException e)
		 {
		     System.out.println("Error:"+e);
		     disconnect();
		 }
		 if(msg==null)                           //如果从服务器传来的信息为空则断开此次连接
		 {
		    client=null;              
		    soc=null;
		    friendlist.clear();
		 }
		 StringTokenizer st=new StringTokenizer(msg,":");   //用StringTokenizer类来实现读取分段字符
		 String keyword=st.nextToken();                     //读取信息头即关键字用来识别是何种信息

		 if(keyword.equals("PEOPLE"))                      //如果是PEOPLE则是服务器发来的客户连接信息
		 {                                                 //主要用来刷新客户端的在线用户列表
			 String []shortstring=msg.split(":");
			 boolean temp[]=new boolean[count];
			 int k;
			 for(int j=0;j<count;j++)
	      	 { 
			    for(k=1;k<shortstring.length;k++)
			    {
			       if(membernum[j].equals(shortstring[k]))
			       {
			      	  temp[j]=true;
			      	  break;
			       }
			       else
			       {
			          temp[j]=false;
			       }  
			    }
			 }
			 for(int p=0;p<count;p++) 
			 {
			    String []shortstring1=friendlist.getItem(p).split("(");
			    if(temp[p]==true)
			    {   if(!shortstring1[shortstring1.length-1].equals("在线)"))
	      	       {
		      	       if(shortstring1[shortstring1.length-1].equals("离线)"))
		      	          friendlist.replaceItem(friendlist.getItem(p).replaceAll("(离线)","(在线)"),p);
		      	       else
		      	          friendlist.replaceItem(friendlist.getItem(p)+"(在线)",p);
	      	       }
	      	    }
	      	    else if(temp[p]==false)
	      	    {   if(!shortstring1[shortstring1.length-1].equals("离线)"))
	      	       {
	      	           if(shortstring1[shortstring1.length-1].equals("在线)"))
	      	              friendlist.replaceItem(friendlist.getItem(p).replaceAll("(在线)","(离线)"),p);
	      	           else
	      	              friendlist.replaceItem(friendlist.getItem(p)+"(离线)",p);
	      	       }
	      	    }
	      	 }
			     
	     }
		 else if(keyword.equals("FRIEND"))                
         {
			 String usr=st.nextToken();
			 String msg1=st.nextToken();
			 String msg2=st.nextToken("\0");
			 boolean at=true;
			 System.out.println(usr+msg1+msg2);
			 if(i!=0)
		     {
		     	for(int j=0;j<i;j++)
			    { 
			      System.out.println(usr+login[j].reciver);
			      if(usr.equals(login[j].reciver)==true)
			      {
			         if(login[j].isVisible()==false)  
			            login[j].frame.setVisible(true);
			         login[j].chatarea.append(msg1+msg2);
			         login[j].chatarea.append("\n");
			         at=false;
			         break;
			      }
			   
			    }
			  }
			  if(at==true)
			  {
			  	 for(int c=0;c<count;c++)
		         if(usr.equals(membernum[c]))
		         {
			        String []shortstring=friendlist.getItem(c).split("(");
			        String re=shortstring[0];
			        String petname="";
			        for(int g=0;g<count;g++)
		            {
		      	       if(membernum[g].equals(clientname))
		      	       {   
		      	         petname=friendlist.getItem(g);
		      	         break;
		      	       }
		            } 
			        StringTokenizer st1=new StringTokenizer(petname,"(");
		            petname=st1.nextToken();
		            System.out.println(usr+clientname+re+petname);
			        login[i]=new chatarea(usr,clientname,re,petname,dis,dos);
			        login[i].chatarea.append(msg1+msg2);
			        login[i].chatarea.append("\n");
		            login[i++].frame.setVisible(true);
		            break;
			      }
			   }
	     }
		 else if(keyword.equals("MEMBER"))                
         {
			 friendlist.clear();
			 String []shortstring=msg.split(":");
		     for(int j=1;j<shortstring.length;j++)
			 {
			 	if(j%3==1)
			 	   membernum[count++]=shortstring[j];
			 	friendlist.add(shortstring[++j]+"("+shortstring[++j]+")");
			 }   
		 }
		 else if(keyword.equals("QUIT"))                //如果关键字是QUIT则是服务器关闭的信息
		                                                //用来切断此次连接
		 {
		     System.out.println("Quit");
		     try
		     {
	             client.stop();
			     client=null;
			     soc.close();
			     soc=null;
             }
             catch(IOException e)
             {
             	System.out.println("Error:"+e);
             	disconnect();
             }
			 friendlist.clear(); 
		 }
	  }
   }     //end of run method
   
   public void disconnect()                                         //客户端点击断开连接要运行的方法
   {
      if(soc!=null)
	  {
	     try
		 {
			dos.writeUTF("QUIT");                                    //用输出流发送QUIT信息通知服务器断开此次通信
			soc.close();                                           //关闭套接字
		    soc=null;
         }
		 catch(IOException e)
		 {
		     System.out.println("Error:"+e);
		 }
	  }// end of if
   }
}

⌨️ 快捷键说明

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