聊天图形化界面

前两天做了一个简单的聊天界面,可以发送消息,清空聊天记录,查看聊天记录,给对方发送震动,挺有意思的~

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 GuiChat extends Frame {
	private TextField tf;
	private Button send;
	private Button log;
	private Button clear;
	private Button shake;
	private TextArea viewText;
	private TextArea sendText;
	private DatagramSocket socket;
	private BufferedWriter bw;

/**
 *Gui聊天界面 
 */
	
	public GuiChat() {
		init();
		southPanel();
		centerPanel();
		event();
	}
	
	/*添加事件*/
	public void event() {
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				try {
					socket.close();
					bw.close();
				} catch (IOException e1) {
					
					e1.printStackTrace();
				}
				System.exit(0);
			}
		});
		
		send.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					send();                      //发送
				} catch (Exception e1) {
					
					e1.printStackTrace();
				}
			}

			
		});
		
		log.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					logFile();                  //登录
				} catch (IOException e1) {
					
					e1.printStackTrace();
				}
			}
			
		});
		
		clear.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				viewText.setText("");        //清屏
			}
		});
		
		shake.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					send(new byte[]{-1},tf.getText());       //震动 
				} catch (IOException e1) {
					
					e1.printStackTrace();
				}
			}

		});
		
		sendText.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER) {
					try {
						send();                         //Enter键发送
					} catch (Exception e1) {
						
						e1.printStackTrace();
					}
				}
			}
		});
	}
	
	private void shake() {
		int x = this.getLocation().x;         //获取横坐标位置
		int y = this.getLocation().y;         //获取纵坐标位置 
		
		for(int i = 0;i < 10; i++) {
			try {
				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) {
				
				e.printStackTrace();
			}
		}
	}
	
	private void logFile() throws IOException {
		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 IOException {
		DatagramPacket packet = 
				new DatagramPacket(arr, arr.length, InetAddress.getByName(ip),9999);
		socket.send(packet);
		
	}
	private void send() throws Exception {
		String message = sendText.getText();   //获取发送区的内容
		String ip = tf.getText();             // 获取ip地址
		
		ip = ip.trim().length() == 0 ? "255.255.255.255" : ip;   //trim是去掉空格
		
		send(message.getBytes(),ip);
		
		String time = getCurrentTime();   //获取当前时间
		String str = time + " 我对 " + (ip.equals("255.255.255.255") ? "所有人" : ip) +"说:\r\n" + message + "\r\n\r\n";     //alt + shift + L  抽取局部变量
		viewText.append(str);         //将信息添加到显示区域中
		bw.write(str);         //将信息写到数据库中(config文档中)
		sendText.setText("");
		
		
	}
	
	private String getCurrentTime() {
		Date d = new Date();        //创建当前日期对象
		SimpleDateFormat sdf = new SimpleDateFormat("yy年MM月dd日 HH:mm:ss");
		return sdf.format(d);         //将时间格式化
	}
	public void centerPanel() {
		Panel center = new Panel();
		viewText = new TextArea();
		sendText = new TextArea(5,1);
		center.setLayout(new BorderLayout());   //设置为边界布局管理器
		
		center.add(sendText,BorderLayout.SOUTH);
		center.add(viewText,BorderLayout.CENTER);
		
		viewText.setEditable(false);
		viewText.setBackground(Color.WHITE);   //设置背景颜色
		
		sendText.setFont(new Font("xxx",Font.PLAIN, 15));
		viewText.setFont(new Font("xxx",Font.PLAIN, 15));
		this.add(center,BorderLayout.CENTER);
	}
	public void southPanel() {
		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);  //将Panel放在Frame的南边
	}
	public void init() {
		this.setLocation(500, 50);
		this.setSize(400, 600);
		new Receive().start();       //先开启receive线程
		try {
			socket = new DatagramSocket();
			bw = new BufferedWriter(new FileWriter("config.txt",true));      //true 需要在尾部追加,而不是清空掉
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		
		this.setVisible(true);
	}
	
	private class Receive extends Thread {      //接收和发送需要同时执行,所以定义成多线程的
		public void run() {
			try {
				DatagramSocket socket = new DatagramSocket(9999);
				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) {  // 如果发过来的数组第一个存储的值是-1,而且数组长度是1
						shake();   //调用震动方法
						continue;  //终止本次循环,继续下次循环,因为震动后不需要执行下面的代码
					}
					
					String message = new String(arr,0,len);  //转换成字符串
				
					String time = getCurrentTime();
					String ip = packet.getAddress().getHostAddress();  // 获取IP地址
				
					String str = time + " " + ip + " 对我说:\r\n" + message + "\r\n\r\n";
					viewText.append(str);
					bw.write(str);
				}
			} catch (Exception e) {
				
				e.printStackTrace();
			} 
		}
	}
	
	public static void main(String[] args) {
		new GuiChat();
	}

}
把运行界面也贴出来看看吧,第二张是聊天记录的

聊天图形化界面_第1张图片聊天图形化界面_第2张图片

你可能感兴趣的:(聊天图形化界面)