一对一聊天程序的实现

TestServer.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class TestServer extends JFrame implements ActionListener {
	DataInputStream dis;
	DataOutputStream dos;
	JTextField tf;
	JTextArea ta;
	public TestServer(){
		this.setTitle("聊天程序服务端");
		JScrollPane jp = new JScrollPane();
		ta = new JTextArea(10,10);
		Panel p = new Panel();
		tf = new JTextField(20);
		JButton b = new JButton("发送");
		b.addActionListener(this);
		tf.addActionListener(this);
		p.add(tf);
		p.add(b);
		jp.setViewportView(ta);
		this.add(jp);
		this.add("South",p);
		this.setSize(350,250);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		tf.requestFocus();
		this.connect();//建立连接
		this.createReadThread();//启动接受信息线程
		
	}
	public void connect(){
		try{
			ServerSocket ss = new ServerSocket(911);//准备通信端口
			Socket s2 = ss.accept();
			InputStream is = s2.getInputStream();
			dis = new DataInputStream(is);//准备输入流
			OutputStream os =s2.getOutputStream();
			dos = new DataOutputStream(os);//准备输出流
		}catch(IOException e){
			System.out.println("连接服务器故障");
		}
	}
	public void createReadThread(){
		ReadThread rt = new ReadThread(this.ta,this.dis);
		rt.start();
	}
	public void actionPerformed(ActionEvent e){
			try{
				String s = tf.getText();
				dos.writeUTF(s);
				ta.append("自己说; "+s	);
				ta.append("\n");
				tf.setText("");
				tf.requestFocus();
			}catch(IOException e1){
				e1.printStackTrace();
			}
	}
	public static void main(String[] args){
		new TestServer();
	}
}
Login.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class Login extends JFrame implements ActionListener {
	JLabel l1;
	JLabel l2;
	JTextField tf1;
	JTextField tf2;
	JButton b1;
	public Login(){
		l1=new JLabel("请输入你的昵称:");
		l2=new JLabel("请设置服务器地址:");
		tf1=new JTextField("文锐");
		tf2=new JTextField("127.0.0.1");
		tf1.addActionListener(this);
		tf2.addActionListener(this);
		b1=new JButton("Login");
		b1.addActionListener(this);
		JPanel p1 = new JPanel();
		p1.add(l1);
		p1.add(tf1);
		JPanel p2 =new JPanel();
		p2.add(l2);
		p2.add(tf2);
		this.add(p1,"North");
		this.add(p2);
		this.add(b1,"South");
		this.setSize(300,200);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
		
	}
	public void actionPerformed(ActionEvent e){
		if(!tf1.getText().equals("")&&!tf2.getText().equals("")){
			TestClient c=new TestClient(tf1.getText(),tf2.getText());//启动客户端聊天程序
			this.setVisible(false);
		}
	}
	public static void main(String[] args){
		new Login();
	}

}

TestClient.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class TestClient extends JFrame implements ActionListener {

	DataInputStream dis;
	DataOutputStream dos;
	JTextField tf;
	JTextArea ta;
	String s11,s22;
	public TestClient(String s1,String s2) {
		this.setTitle("聊天程序客户端");
		JScrollPane jp =new JScrollPane();
		ta = new JTextArea(10,10);
		JPanel p =new JPanel();
		tf = new JTextField(20);
		JButton b =new JButton("发送");
		b.addActionListener(this);
		tf.addActionListener(this);
		p.add(tf);
		p.add(b);
		jp.setViewportView(ta);
		this.add(jp);
		this.add(p,"South");
		
		this.setSize(350,250);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.s11 = s1;
		this.s22 = s2;
		this.setVisible(true);
		tf.requestFocus();
		this.connect();
		this.createReadThread();
		
	}
	
	public void connect(){
		try{
			Socket s2 = new Socket(s22,911);
			InputStream is = s2.getInputStream();
			dis = new DataInputStream(is);
			OutputStream os =s2.getOutputStream();
			dos = new DataOutputStream(os);
			
		}catch(IOException e){
			System.out.println("连接服务器故障");
			
		}
	}
	
	public void createReadThread(){
		ReadThread rt = new ReadThread(this.ta,this.dis);
		rt.start();
	}

	public void actionPerformed(ActionEvent e) {
		try{
			String s =tf.getText();
			dos.writeUTF(s11+"说: "+ s);
			ta.append("自己说: " +s);
			ta.append("\n");
			tf.setText("");
			tf.requestFocus();
		}catch(IOException e1){
			e1.printStackTrace();
		}
		
	}
	

}

ReadThread.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class ReadThread extends Thread {

	JTextArea ta;
	DataInputStream dis;
	public ReadThread(JTextArea t,DataInputStream d){
		this.ta = t;
		this.dis =d;
	}
	public void run(){
		try{
			while(true){
				ta.append("对方说: "+dis.readUTF());//接受对方发送的信息
				ta.append("\n");
				
			}
		}catch(IOException e){
			System.out.println("连接中断! ");
		}
	}
}


你可能感兴趣的:(java)