黑马程序员---java局域网聊天工具开发

----------- android培训java培训、java学习型技术博客、期待与您交流! ------------
package cn.itcast.day12;

import java.awt.Button;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
 * @author 王震阳 931964335@qq.com
 * @version 1.0
 * 
 * 该程序使用了UDP非可靠方式进行网络数据的传输,利用java.util.awt实现图像化界面.
 * 该程序可以在局域网中实现多人聊天室的功能
 * 经过多台机子测试,该程序在64位win7操作系统上不能很好的运行,只能接受到数据,而无法发送数据,具体
 * 原因还未搞明白.
 */
class ChatDemo2//带图形化界面的聊天程序
{
	private boolean flag=false;
	public void setFlag(boolean flag)
	{
		this.flag=flag;
	}
	public boolean getFlag()
	{
		return flag;
	}
	private  Frame frame=new Frame("java 版ChatRoom @antaojin");
	private Panel panel1=new Panel();
	private Panel panel2=new Panel();
	private Panel panel3=new Panel();
	private TextArea text1=new TextArea("",20,39,TextArea.SCROLLBARS_VERTICAL_ONLY);
	private TextArea text2=new TextArea("",5,39,TextArea.SCROLLBARS_VERTICAL_ONLY);
	private String pathName;//打开文件时保存文件的路径
	private String IP="192.168.1.255";//设置自己的IP
	public void setIP(String IP)
	{
		this.IP=IP;
	}
	public String getIP()
	{
		return IP;
	}
	public void setPathName(String pathName)
	{
		this.pathName=pathName;
	}
	public String getPathName()
	{
		return pathName;
	}
	public String getText2()
	{
		return text2.getText();
	}
	public void appendText1(String str)
	{
		text1.append(str);
	}
	private Button but1=new Button("发送");
	//private Button but2=new Button("选择文件");
	//private Button but3=new Button("发送文件");
	private Button but4=new Button("设置IP");
	public static void main(String[] args) 
	{
		ChatDemo2 cd2=new ChatDemo2();
		cd2.init();//初始化图形界面
		ChatSend cs=new ChatSend(cd2);//建立发送客户端任务
		ChatRec cr=new ChatRec(cd2); //建立接受客户端任务
		//SendFile sf=new SendFile(cd2);//建立文件发送任务,已经删掉此功能
		//RecFile rf=new RecFile(cd2);//建立文件接受任务,已经删除此功能
		Thread t1=new Thread(cs);//创建线程,挂带发送任务
		Thread t2=new Thread(cr);//创建线程,挂带接受任务
		//Thread t3=new Thread(sf);
		//Thread t4=new Thread(rf);
		t1.start();              //启动发送线程
		t2.start();		//启动接受线程
		//t3.start();
		//t4.start();
	}
	private  void init()//初始化图形界面
	{
		//初始化Frame,并增加关闭动作监听器
		frame.setBounds(800,60,300,550);  //设置默认界面大小和位置
		frame.setBackground(new Color(255, 240 ,245));//设置背景色
		frame.setVisible(true);                      //设置界面为可见
		//为界面增加事件监听器
		frame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e)
			{
				System.out.println(e);
				System.exit(0);
			}
		});
		frame.setLayout(new FlowLayout());//设置界面布局方式
		text1.setForeground(new Color(93, 71 ,139));//设置文本字体颜色
		panel1.add(text1);
		text1.setEditable(false);
		panel2.add(text2);
		text2.setBackground(new Color(240, 255 ,240));
		text2.setForeground(new Color(0, 0, 255));
		//为text2增加事件监听
		text2.addTextListener(new TextListener(){
			public void textValueChanged(TextEvent e)
			{
				frame.setTitle("正在输入...");
			}
		});
		panel3.add(but1);
		//panel3.add(but2);
		//panel3.add(but3);
		panel3.add(but4);
		frame.add(panel1);
		frame.add(panel2);
		frame.add(panel3);
		//为but1增加监听器
		but1.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{				 
				frame.setTitle("java 版聊天室 antaojin");
				if(text2.getText().length()<1)
					return;
				setFlag(true);
				try
				{
					Thread.sleep(10);
					text2.setText("");
				}
				catch (Exception ee){}
			   
			   frame.setTitle("java 版聊天室 antaojin");
			}
		});
		//为but2增加响应,选择文件
		//为but3增加响应,发送文件
		//为but4增加设置IP时间
		but4.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e)
			{
				final Dialog dialog=new Dialog(frame,"设置您的IP地址 antaojin",false);
				final TextField tf=new TextField("127.0.0.1");
				dialog.setBounds(500,300,300,90);
				dialog.add(tf);
				dialog.setVisible(true);
				dialog.setLayout(new FlowLayout());
				dialog.setBackground(new Color(0 ,245 ,255));
				Button but=new Button("确定");
				dialog.add(but);
				dialog.addWindowListener(new WindowAdapter(){
					public void windowClosing(WindowEvent e)
					{
						dialog.setVisible(false);
					}
				});
				but.addActionListener(new ActionListener(){
					public void actionPerformed(ActionEvent e)
					{
						setIP(tf.getText());//获取文本中的IP,并设置IP,一般不用
						dialog.setVisible(false);
						text2.append(getIP()+"\r\n");
					}
				});
			}
		});
	}
}
class ChatSend implements Runnable
{
	private ChatDemo2 cd2;
	ChatSend(ChatDemo2 cd2)
	{
		this.cd2=cd2;
	}
	public void run() 
	{
		try
		{
			DatagramSocket ds=new DatagramSocket();
			String str=null;
			while(true)
			{
				if(cd2.getFlag())
				{
					cd2.setFlag(false);
					str=cd2.getText2();
					DatagramPacket dp=new DatagramPacket(str.getBytes(),str.getBytes().length,InetAddress.getByName(cd2.getIP()),12425);
					ds.send(dp);
				}
			}
		}
		catch (Exception e){}
	}
}
class ChatRec implements Runnable
{
	private ChatDemo2 cd2;
	ChatRec(ChatDemo2 cd2)
	{
		this.cd2=cd2;
	}
	public void run()
	{
		try
		{
			DatagramSocket ds=new DatagramSocket(12425);
			byte[]buf=new byte[1024];
			DatagramPacket dp=new DatagramPacket(buf,buf.length);
			while(true)
			{ 
				ds.receive(dp);
				String str=new String(dp.getData(),0,dp.getLength());
				cd2.appendText1(dp.getAddress().getHostName()+"说:\r\n"+str+"\r\n");
			}
		}
		catch (Exception e)	{}
	}
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值