Java serve_ client聊天之client端(合工大java实验四)by峰宇

package client;

import server.Myframe;

public class client {

	public static void main(String[] args) {
		 
		Myframec myframe=new Myframec("客户v放大端端", 400, 50, 600, 600)	;
	}

}

package client;

import java.awt.BorderLayout;
import java.awt.HeadlessException;
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.Socket;
import java.net.UnknownHostException;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyledDocument;

import Messagemy.Messagemy;

public class Myframec extends JFrame {
	DataInputStream dataInputStream;
	DataOutputStream dataOutputStream;
	Socket socket;

	BorderLayout bLayout;
	JLabel areaip;
	JLabel areaport;
	JEditorPane jEditorPaneport;
	JEditorPane jEditorPaneip;
	JButton jButtonP;

	JLabel areaxia;
	JEditorPane jEditorPanexia;
	JButton jButtonPxia;

	JTextPane Jzhongshow;

	public Myframec(String title, int x, int y, int w, int h)
			throws HeadlessException {
		super(title);
		setLocation(x, y);
		setSize(w, h);
		init();
		this.setVisible(true);

	}

	private void init() {
		areaip = new JLabel("IP");
		jEditorPaneip = new JEditorPane();
		areaport = new JLabel("端口");
		jEditorPaneport = new JEditorPane();
		jButtonP = new JButton("确定");
		jButtonP.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// int p=10000;
				// String ip="127.0.0.1";
				int p = Integer.valueOf(jEditorPaneport.getText().toString());
				String ip = jEditorPaneip.getText().toString();
				try {
					Clientchat clientchat = new Clientchat(ip, p);
					clientchat.start();
				} catch (UnknownHostException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});

		JPanel jshang = new JPanel();
		jshang.add(areaip);
		jshang.add(jEditorPaneip);
		jshang.add(areaport);
		jshang.add(jEditorPaneport);
		jshang.add(jButtonP);

		Jzhongshow = new JTextPane();
		JScrollPane jScrollPanezhong = new JScrollPane(Jzhongshow);
		Jzhongshow.setEditable(false);

		areaxia = new JLabel("Say");
		jEditorPanexia = new JEditorPane();
		jButtonPxia = new JButton("发送");
		jButtonPxia.addActionListener(new sendmessege());
		JPanel jxia = new JPanel();
		jxia.add(areaxia);
		jxia.add(jEditorPanexia);
		jxia.add(jButtonPxia);

		add(jshang, BorderLayout.NORTH);
		add(jScrollPanezhong, BorderLayout.CENTER);
		add(jxia, BorderLayout.SOUTH);

	}

	public class sendmessege implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			String message = jEditorPanexia.getText().toString();
			Date date = new Date();
			String namem = "我";
			Messagemy messagemy = new Messagemy(namem, date, message);

			try {
				dataOutputStream.writeUTF(message);
				dataOutputStream.flush();
				insertmaserge(messagemy);
				jEditorPanexia.setText("");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

	}

	private void insertmaserge(Messagemy messagemy) {
		StyledDocument doc = Jzhongshow.getStyledDocument();
		SimpleAttributeSet attr = new SimpleAttributeSet();
		String s = messagemy.getDate() + " " + '\n';
		String p = messagemy.getName() + "  : " + messagemy.getMessage() + '\n';
		try {
			doc.insertString(doc.getLength(), s, attr);
			doc.insertString(doc.getLength(), p, attr);
			doc.insertString(doc.getLength(), "\n", attr);
			Jzhongshow.setCaretPosition(Jzhongshow.getDocument().getLength());

		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public class Clientchat extends Thread {

		public Clientchat(String ip, int p) throws UnknownHostException,
				IOException {
			socket = new Socket(ip, p);
			dataInputStream = new DataInputStream(socket.getInputStream());
			dataOutputStream = new DataOutputStream(socket.getOutputStream());
		}

		@Override
		public void run() {
			while (true) {
				try {
					String s = dataInputStream.readUTF();
					Date date = new Date();
					String namem = "他";
					Messagemy messagemy = new Messagemy(namem, date, s);
					insertmaserge(messagemy);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}

	}

}

package Messagemy;

import java.util.Date;

public class Messagemy {
	private String name;
	private Date date;
	private String message;

	public Messagemy(String name, Date date, String message) {
		this.name = name;
		this.date = date;
		this.message = message;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值