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

📄 chat.java

📁 本程序实现了QQ聊天的功能,模拟QQ聊天程序,实现了其基本功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import  java.awt.*;
import  java.awt.event.*;
import  java.net.*;
import  java.applet.*;
import  java.applet.Applet;
import  java.applet.AudioClip;
import  javax.swing.*;
import  java.applet.*;
public  class  Chat extends Applet  implements  ActionListener
{ String  compare[]=new  String[20];
	Frame  f=new  Frame("我们的聊天室");
	TextField   tfip=new  TextField(14);
	TextField   tf=new  TextField(14);
	TextArea   tfData=new  TextArea(20,30);
	TextField  userField=new  TextField(10); 
	
	private     Choice  choiceButton,songsButton,colorButton;
	
	private     Button   checkChat,ok2,ok3,exit;
	Button btnPlay = new Button ("播放");
    Button btnStop = new Button("停止");
    Button   sendButton,out;
    Button   input,yes,no,ok,del,reply;
    
	
	String  item="所有人";
	
	AudioClip sndDemo;
    Image  img=null;
     DatagramSocket   ds;
    
     List  lst=new  List(1);
     List  name=new  List(20);
     List  talk=new  List(10);
     
    
     
     Label  l1,l2,l3,l5,l6,l4;
     
     int  biaoZhi=0;
     int  i=0,q=0;
     int  symbol=0;
    
    
     Dialog  dlg=new  Dialog(f,"登入",true);
     Dialog  dl =new  Dialog(f,"确认",true);
     Dialog  d  =new  Dialog(f,"私聊窗口",false) ;  
     Dialog  refuse=new  Dialog(f,"确认",true);
     Dialog  refuse2=new  Dialog(f,"确认",true);
     Dialog  wrong=new   Dialog(f,"警告",true);
     public  Chat(){
     
     //	Container  cp=getContentPane();
     //NewPanel  pf=new NewPanel();
     //cp.add(pf);
     	for(int  s=0;s<20;s++)
     		compare[s]="2";
     	try 
     	{
     		ds=new  DatagramSocket(3000);
     	}catch(Exception  ex){ex.printStackTrace();}
     
     	new Thread(new  Runnable()
     	{public  void  run()
     	{byte  buf[]=new  byte[1024];
     	DatagramPacket  dp=new  DatagramPacket(buf,1024);
     	
     	
     	while (true)
     	{try {
     		ds.receive(dp);
     
     	if(buf[0]==4&&buf[1]==8)
     		{
     			d.dispose();
     		refuse2.setBounds(240,150,150,150);
     		refuse2.setVisible(true);
     		d.dispose();
     	}else
     	
     	
     		if(buf[0]==3&&buf[1]==7)
     		{
     			d.dispose();
     		refuse2.setBounds(240,150,150,150);
     		refuse2.setVisible(true);
     	}else
     	if(buf[0]==3&&buf[1]==5)
     	{System.out.println("zdj");
     	biaoZhi=1;
     		for(  q=0;q<20;q++)
     		
     			if(compare[q].equals(dp.getAddress().getHostAddress()))
     		
     		   { 
     		   d.setTitle("与"+name.getItem(q)+"的私聊窗口");
     		   System.out.println("fsd");
     			d.setBounds(240,150,450,350);
     			d.setVisible(true);break;}
     			
     		}
     	else
     	if(buf[0]==9&&buf[1]==8)
     	{biaoZhi=0;
     		refuse.setBounds(240,150,150,150);
     		refuse.setVisible(true);
     	}	
     	else
     	if(buf[0]==2&&buf[1]==3)
     	{for(  q=0;q<20;q++)
     		
     			if(compare[q].equals(dp.getAddress().getHostAddress()))
     			
     	{	dl.setTitle(name.getItem(q));
     		dl.setBounds(240,150,150,150);
     		dl.setVisible(true);break;}
     	
     	}
     	else
     		if(buf[0]==9&&buf[1]==0)
     		{
     		
     	
     		for(  q=0;q<20;q++)
     		{
     			if(compare[q].equals(dp.getAddress().getHostAddress()))
     		{lst.add(name.getItem(q)+"退出聊天室!",0);
     	
     			name.delItem(q);
     		compare[q]="2";
     		symbol=1;
     		break;}}
     	}
     		
     		
     
     	else 
     	{
     		
     		for(  q=0;q<20;q++)
     		{
     			if(compare[q].equals(dp.getAddress().getHostAddress()))
     		break;}
                	if(q==20)
     		{
     				for(i=0;i<20;i++)
     		{if(compare[i].equals("2"))
     		{compare[i]=dp.getAddress().getHostAddress();
     		int  n=dp.getLength();
     		if(buf[12]==7)
     		name.add(new String (buf,0,n-2),i);
     		else
     	{	name.add(new  String(buf,0,dp.getLength()),
     		i);
     		
     		lst.add(new  String(buf,0,dp.getLength())+" ["+dp.getAddress().getHostAddress()+"]   "+"进入聊天室!",
     		0);
     		
     		
     		byte  b[];
           b=userField.getText().getBytes();
           int  t=b.length;
           b[12]=7;
     			
     				try {
     				  dp=new  DatagramPacket(b,b.length,
     				InetAddress.getByName(dp.getAddress().getHostAddress()),3000);
     				
     				ds.send(dp);
     			}
     			
     			catch(Exception  ex){
     				ex.printStackTrace();}
     			}
     	break;}}
     	}
     		  		
     	
     
                      else
        
     		{if(biaoZhi==0)
     			lst.add(name.getItem(q)+ new  String(buf,0,dp.getLength()),0);
     			else
     			talk.add(name.getItem(q)+" 对 你 说 :"+ new  String(buf,0,dp.getLength()),0);}
     	//	else {
     		//lst.add(name.getItem(q) + new  String(buf,0,dp.getLength()),0);
     		
     	
     }
     	}catch (Exception  e){e.printStackTrace();}
     }
  }
}).start();

     }
    

		
     
     
     
     
     
     public  static  void  main(String  []  args)
     { 
     	Chat  chat  =new  Chat();
     	chat.init();
     	 
     	 
   
     }
     	
     public  void  init ()
     { 
     
         //img=f.getToolkit().getImage("D:\\1.jpg");		
     
      
     exit=new  Button("清屏");
     del=new   Button("删除");
     
     
     
     l3=new Label("是否同意私聊?");
     Panel  p3=new  Panel();
     Panel  p4=new  Panel();
     Panel  p6=new  Panel();
     Panel  p5=new  Panel();
     Panel  p7=new  Panel();
     //Panel  p5=new  Panel();
      sendButton =new  Button("发送");
      yes        =new Button("同意");
      no         =new Button("拒绝");
      ok         =new Button("确定");
      reply      =new  Button("确定");
      ok2        =new Button("确定");
      out        =new Button("退出");
            
      dl.add("South",p3);
      dl.add("Center",l3);
      p3.add("West",yes);
      p3.add("East",no);
      
     	checkChat    =new  Button("私聊");
     	choiceButton =new Choice();
        colorButton  =new  Choice();
        songsButton  =new  Choice();
     
     choiceButton.addItem("表情");
     choiceButton.addItem("微笑");
     choiceButton.addItem("兴奋");
     choiceButton.addItem("高兴");
     choiceButton.addItem("严肃");
     choiceButton.addItem("神秘");
     choiceButton.addItem("大声");
     choiceButton.addItem("生气");
     choiceButton.addItem("愤怒");
     
     colorButton.addItem("黑色");
     colorButton.addItem("橙色");
     colorButton.addItem("粉红");
     colorButton.addItem("青蓝");
     colorButton.addItem("品红");
     colorButton.addItem("黄色");
     colorButton.addItem("灰色");
     colorButton.addItem("绿色");
     colorButton.addItem("蓝色");
     
     songsButton.addItem("背景音乐");
     songsButton.addItem("歌曲1");
     songsButton.addItem("歌曲2");
     songsButton.addItem("歌曲3");
     songsButton.addItem("歌曲4");
     songsButton.addItem("歌曲5");
     
     
     
     
     	l1=new Label("用户名:");
     	
     	TextField  passLabel=new  TextField(10);
     	l2=new  Label("密码");
     	l5=new Label("对方拒绝私聊");
     	l6=new Label("对方退出私聊");
     	l4=new  Label("请选择发送对象");
     	input  =new  Button("登入");
     	ok3=new Button("确定");
     	refuse.setLayout(new  FlowLayout(FlowLayout.LEFT,10,10));
        refuse.add(l5);
        refuse.add("South",ok2);
        refuse2.setLayout(new  FlowLayout(FlowLayout.LEFT,10,10));
        refuse2.add(l6);
         refuse2.add("South",ok3);
         
         
        wrong.setLayout(new  FlowLayout(FlowLayout.LEFT,10,10));
        wrong.add("Center",l4);
        wrong.add("South",reply);
     //regist=new  Button("登入");
        f.setLayout(new  BorderLayout());
     	f.setSize(800,600);
     	//f.add("Center",lst);
     	//lst.setBackground(Color.GREEN);
     	
     	Panel  p=new  Panel();
     	Panel  p2=new  Panel();
     	p.setLayout(new  BorderLayout());
     	d.setLayout(new  BorderLayout());
     	d.add("Center",p4);
     	
     //	d.add("South",p5);
        p4.setLayout(new  GridLayout(2,1));
     	p4.add(talk);
     	p4.add(tf);
     //name.setBackground(Color.YELLOW);
     	//p.add("West",tfip);
     	
     	//p.add("Center",tfData);
     	//p.add("East",input);
        f.setLayout(new  BorderLayout());
     	f.add("Center",p2);
     	//f.add("South",p2);
     	f.add("West",name);
     	//p6.add("Center",p2);
     	f.add("South",p5);
     	p2.setLayout(new  GridLayout(2,1));
     	
     	p2.add(lst);
     	p2.add(tfData);
     
     	p5.add(choiceButton);
     	p5.add(colorButton);
     	p5.add(songsButton);
     	p5.add(btnPlay);
     	p5.add(btnStop);
     	p5.add(checkChat);
     	
     
     	p5.add(sendButton);
     	p5.add(del);
     	p5.add(exit);
     	p5.add(out);
     	choiceButton.setBackground(new  Color(205,190,180));
     	colorButton.setBackground(new  Color(205,190,180));
     	songsButton.setBackground(new  Color(205,190,180));
     	
     	btnPlay.setBackground(new  Color(185,209,185));
     	btnStop.setBackground(new  Color(185,209,185));
     	
     	checkChat.setBackground(new  Color(185,195,205));
     	sendButton.setBackground(new  Color(185,195,205));
     	del.setBackground(new  Color(185,195,205));
     	exit.setBackground(new  Color(185,195,205));
     	out.setBackground(new  Color(185,195,205));
     	
     	p5.setBackground(Color.lightGray);
     	//f.setBackground(Color.magenta);

⌨️ 快捷键说明

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