java--聊天工具

package day26;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Font;
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.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo_4 extends Frame{
private TextField tf;
private Button send;
private Button log;
private Button clear;
private Button shake;
private TextArea viewText;
private TextArea senText;
private DatagramSocket socket;
private BufferedWriter bw;
//private String ip ;
public Demo_4() {
init();
southPannel();
centerPanel();
event();

}
private void event() {
	// TODO Auto-generated method stub
	this.addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
			socket.close();
			try {
				bw.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			System.exit(0);
		}
	});
	send.addActionListener(new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			try {
				send();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}			
	});
	log.addActionListener(new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			try {
				logFile();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		
		
	});
	clear.addActionListener(new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			viewText.setText("");
		}
		
	});
	shake.addActionListener(new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			//shake();
			try {
				send(new byte[] {-1},tf.getText());
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

		
		
	});
	senText.addKeyListener(new KeyAdapter() {
		public void keyReleased(KeyEvent e) {
			if(e.getKeyCode()== KeyEvent.VK_ENTER ) {
				try {
					send();
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		}
	});
}
private void shake() {
	// TODO Auto-generated method stub
	int x =this.getLocation().x;
	int y= this.getLocation().y;
	for(int i=0;i<10;i++) {
	try {
		Thread.sleep(20);
		this.setLocation(x+20,y+20);
		Thread.sleep(20);
		this.setLocation(x+20,y-20);
		Thread.sleep(20);
		this.setLocation(x-20,y+20);
		Thread.sleep(20);
		this.setLocation(x-20,y-20);
		Thread.sleep(20);
		this.setLocation(x,y);
	} catch (InterruptedException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	}
}
private void logFile() throws IOException {
	// TODO Auto-generated method stub
	bw.flush();
	FileInputStream fis =new FileInputStream("config.txt");
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	int len;
	byte[] arr = new byte[8192];
	while((len=fis.read(arr)) != -1) {
		baos.write(arr,0,len);
	}
	String str =baos.toString();
	viewText.setText(str);
	fis.close();
}
private void send(byte[] arr,String ip) throws Exception {
	DatagramPacket  packet= 
			new DatagramPacket(arr,arr.length,InetAddress.getByName(ip), 9999);
	socket.send(packet);
}
private void send() throws Exception {
	// TODO Auto-generated method stub
	String message = senText.getText();
	String ip = tf.getText();
	ip=ip.trim().length() ==0 ? "255.255.255.255" :ip;
	
	send(message.getBytes(),ip);
	String time =getCurrentTime();
	String str= time+" 我对:"+(ip.equals("255.255.255.255")? "所有人" :ip) +"\r\n" + message+"\r\n";
	viewText.append(str);
	bw.write(str);
	senText.setText("");
	
}
private String getCurrentTime() {
	// TODO Auto-generated method stub
	Date d =new Date();
	SimpleDateFormat sdf =new SimpleDateFormat("YY年MM月dd月 HH:ss:ss");
	return sdf.format(d);
}
private void centerPanel() {
	// TODO Auto-generated method stub
	Panel center = new Panel();
	 viewText =new TextArea();
	 senText =new TextArea(5,1);
	center.setLayout(new BorderLayout());
	center.add(senText,BorderLayout.SOUTH);
	center.add(viewText,BorderLayout.CENTER);
	viewText.setEnabled(false);
	viewText.setBackground(Color.white);
	senText.setFont(new Font("xxx",Font.PLAIN,15));
	viewText.setFont(new Font("xxx",Font.PLAIN,15));
	this.add(center,BorderLayout.CENTER);
}
public void southPannel() {
	// TODO Auto-generated method stub
	Panel south =new Panel();
	tf =new TextField(15);
	tf.setText("127.0.0.1");
	 send =new Button("发送");
	 log =new Button("记录");
	 clear =new Button("清屏");
	 shake =new Button("震动");
	south.add(tf);
	south.add(send);
	south.add(log);
	south.add(clear);
	south.add(shake);
	this.add(south,BorderLayout.SOUTH);
	
	
	
}
public void init() {
	this.setLocation(500,50);
	this.setSize(400, 600);
	new Recevice().start();
	  try {
		socket = new DatagramSocket();
		bw =new BufferedWriter(new FileWriter("config.txt",true));
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	this.setVisible(true);
}
private class  Recevice extends Thread{
	public void run() {
		try {
			DatagramSocket socket= new DatagramSocket(8888);
			DatagramPacket packet= new DatagramPacket(new byte[8192],8192);
			while(true) {
			socket.receive(packet);
			byte[] arr = packet.getData();
			int len=packet.getLength();
			if(arr[0] == -1 && len ==1) {
				shake();
				continue;
			}
			String message =new String(arr,0,len);
			String time =getCurrentTime();
			String ip = packet.getAddress().getHostAddress();
			String string = time+" :"+ip+"\r\n";
			viewText.append(string );
			bw.write(string);
			viewText.append(message +"\r\n");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
}
public static void main(String[] args) {
	// TODO Auto-generated method stub
	new Demo_4();
	
}

}

上一篇:java包的概念


下一篇:java生成随机验证码