QQ界面简单制作

package com.qyl.TCP;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;

public class Client extends JFrame{

	private TextArea ta;
	private TextField tf;
	private Button bt;
	private Socket socket;
	private DataOutputStream out;
	private DataInputStream in;


	public DataInputStream getIn() {
		return in;
	}
	
	public TextArea getTa() {
		return ta;
	}







	/**
	 * 初始化界面
	 */
	private void initUI() {
		ta = new TextArea();
		tf = new TextField();
		bt = new Button("send");
		
		//添加监听事件
		addListener();
		
		Panel p = new Panel();
		p.setLayout(new BorderLayout());
		p.add(tf,  BorderLayout.CENTER);
		p.add(bt, BorderLayout.EAST);
		
		this.add(p, BorderLayout.SOUTH);
		this.add(ta, BorderLayout.CENTER);
		
		this.setTitle("花花");
		this.setSize(300, 400);
		this.setLocation(300, 400);
		this.setVisible(true);
	}
	
	
	
	private void addListener() {
		bt.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				//先获取字段内容
				String text = tf.getText();
				//清空
				tf.setText(null);
				//聚焦
				tf.requestFocus();
				//把拿到的数据放到文本域
				String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
				
				ta.append("自 己     "+date+"\r\n\t"+ text+"\r\n");
				//把信息发送给对方
				try {
					out.writeUTF(text);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		
	}



	public void initConn() {
		try {
			socket = new Socket(InetAddress.getLocalHost(),8888);
			out = new DataOutputStream(socket.getOutputStream());
			in = new DataInputStream(socket.getInputStream());	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
	public Client() {
		//初始化UI
		initUI();
		//初始化链接
		initConn();
		//启动接收信息的线程
		new ClientThread(this).start();
	}
	
	
	public static void main(String[] args) {
		new Client();

	}

}
package com.qyl.TCP;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JFrame;

public class Service extends JFrame{

	
	
	private TextArea ta;
	private TextField tf;
	private Button bt;
	private ServerSocket serverSocket;
	private Socket accept;
	private DataOutputStream out;
	private DataInputStream in;

	
	public TextArea getTa() {
		return ta;
	}


//	public void setTa(TextArea ta) {
//		this.ta = ta;
//	}


	public DataInputStream getIn() {
		return in;
	}


//	public void setIn(DataInputStream in) {
//		this.in = in;
//	}
	
	
	
	
	
	


	private void initUI() {
		ta = new TextArea();
		tf = new TextField();
		bt = new Button("send");
		
		addListener();
		
		Panel p = new Panel();
		p.setLayout(new BorderLayout());
		p.add(tf,  BorderLayout.CENTER);
		p.add(bt, BorderLayout.EAST);
		
		this.add(p, BorderLayout.SOUTH);
		this.add(ta, BorderLayout.CENTER);
		
		this.setTitle("草草");
		this.setSize(300, 400);
		this.setLocation(1100, 400);
		this.setVisible(true);
	}
	
	
	
	private void addListener() {
		bt.addActionListener(new ActionListener() {
			

			@Override
			public void actionPerformed(ActionEvent e) {
				String text = tf.getText();	
				tf.setText(null);
				tf.requestFocus();
				String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
				ta.append("自 己     "+date+"\r\n\t"+ text+"\r\n");
				try {
					out.writeUTF(text);
				} catch (IOException e2) {
					e2.printStackTrace();
				}
			}
		});
		
	}



	private void initConn(){
		try {
			serverSocket = new ServerSocket(8888);
			accept = serverSocket.accept();
			out = new  DataOutputStream(accept.getOutputStream());
			in = new DataInputStream(accept.getInputStream());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	

	public Service() {
		initUI();
		initConn();
		new ServiceThread(this).start();
	}
	
	public static void main(String[] args) {
		
		new Service();
	}

}
package com.qyl.TCP;

import java.awt.TextArea;
import java.io.DataInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ClientThread extends Thread {
	private Client client;
	
	public ClientThread(Client client){
		this.client = client;
	}
	
	
	@Override
	public void run() {
		try {
			while (true) {
			//拿到发送过来的文本信息
			DataInputStream in = client.getIn();
			String msg = in.readUTF();
			//拿到文本框 把信息写到文本框里面
			TextArea ta = client.getTa();
			String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			ta.append("草草    "+date+"\r\n\t"+ msg+"\r\n");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
package com.qyl.TCP;

import java.awt.TextArea;
import java.io.DataInputStream;

import java.text.SimpleDateFormat;
import java.util.Date;
public class ServiceThread extends Thread{
	
	private Service service;

	public ServiceThread(Service service) {
		this.service = service;	
	}
	
	
	
	@Override
	public void run() {
		try {
			while (true) {
			// 拿到发送过来的文本类容
			DataInputStream in = service.getIn();
			String msg = in.readUTF();
			// 拿到文本框 把信息写到文本框里面
			TextArea ta = service.getTa();
			String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			ta.append("花花     " + date + "\r\n\t" + msg + "\r\n");
			}
			} catch (Exception e) {
			e.printStackTrace();	
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值