一对一界面化简易聊天

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class FrameQ1 extends JFrame{
	
	private static final int Q_WIDTH = 500;
	private static final int Q_HEIGHT = 500;
	private PrintWriter pw = null;
	
	
	public FrameQ1() {
		
		final JTextField jtf = new JTextField(15);
		JButton send = new JButton("发送");
		final JTextArea jta = new JTextArea();
		
		JPanel jp = new JPanel();
		jp.add(jtf);
		jp.add(send);
		
		
		JScrollPane jsp = new JScrollPane(jta);
		this.add(jsp);
		this.add(jp,BorderLayout.SOUTH);
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension de = tk.getScreenSize();
		int x = (int) ((de.getWidth() - Q_WIDTH)/2);
		int y = (int) ((de.getHeight() - Q_HEIGHT)/2);
		this.setLocation(x, y);
		this.setSize(Q_WIDTH,Q_HEIGHT);
		this.setTitle("小美---服务端");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		try {
			ServerSocket server =new  ServerSocket(9999);
			Socket s1 = server.accept();
			final OutputStream os = s1.getOutputStream();
			//pw = new PrintWriter(os);
			send.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String jText1 = jtf.getText();
					if(jText1.equals("")){
						return;
					}
					jta.append("小黑说:"+jText1+"\r\n");
					jtf.setText("");
					PrintWriter pw = new PrintWriter(os);
					pw.write(jText1+"\r\n");
					pw.flush();
				}
			});
			InputStream is = s1.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			
			BufferedReader br = new BufferedReader(isr);
			String str = null;
			
			while((str = br.readLine())!= null){
				jta.append("小美说:"+str+"\r\n");
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		new FrameQ1();
	}
}


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class FrameQ2 extends JFrame{
	
	private static final int Q_WIDTH = 500;
	private static final int Q_HEIGHT = 500;
	
	
	
	public FrameQ2() {
		
		final JTextField jtf = new JTextField(15);
		JButton send = new JButton("发送");
		final JTextArea jta = new JTextArea();
		
		JPanel jp = new JPanel();
		jp.add(jtf);
		jp.add(send);
		
		
		
		
		
		
		
		JScrollPane jsp = new JScrollPane(jta);
		this.add(jsp);
		this.add(jp,BorderLayout.SOUTH);
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension de = tk.getScreenSize();
		int x = (int) ((de.getWidth() - Q_WIDTH)/2);
		int y = (int) ((de.getHeight() - Q_HEIGHT)/2);
		this.setLocation(x, y);
		this.setSize(Q_WIDTH,Q_HEIGHT);
		this.setTitle("小黑----客户端");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		try {
			Socket s2 = new Socket("localhost",9999);
			final OutputStream os = s2.getOutputStream();
			send.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					String jText1 = jtf.getText();
					if(jText1.equals("")){
						return;
					}
					jta.append("小美说:"+jText1+"\r\n");
					jtf.setText("");
					//向服务端写数据
					PrintWriter pw = new PrintWriter(os,true);
					pw.write(jText1+"\r\n");
					pw.flush();
				}
			});
			InputStream is = s2.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			BufferedReader br = new BufferedReader(isr);
			String str = null;
			while((str = br.readLine())!= null){
				jta.append("小黑说:"+str+"\r\n");
			}
			
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		new FrameQ2();
	}
}

先运行服务端再运行客户端

你可能感兴趣的