首页 > 代码库 > java即时在线聊天(一)

java即时在线聊天(一)

 

今天模仿着马士兵老师的视频,做了一个即时在线聊天。此时拿出来与大家分享,做的不太好,请大家凑合着看吧

import java.awt.Frame;public class ChatClient extends Frame {	/**	 * @param args	 */	public static void main(String[] args) {		new ChatClient().lanchFrame();	}	public void lanchFrame() {		this.setLocation(400, 400);		this.setSize(300, 300);		setVisible(true);	}}

效果图

     

以上只是一个简单的布局,没什么实质性代码。下面给大家讲第二部分

import java.awt.BorderLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;public class ChatClient extends Frame {	TextField tftext=new TextField();	TextArea taContent=new TextArea();				public static void main(String[] args) {			new ChatClient().lanchFrame();			}				public void lanchFrame(){			this.setLocation(400, 400);			this.setSize(300, 300);			add(tftext,BorderLayout.SOUTH);			add(taContent,BorderLayout.NORTH);			pack();			setVisible(true);		}		}


第二次比着第一次多了一个输入框。此时只能用右下角的红色按钮关闭。

如图 红色的小正方形                                 


Chat0.3

import java.awt.BorderLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class ChatClient extends Frame {	TextField tftext = new TextField();	TextArea taContent = new TextArea();	public static void main(String[] args) {		new ChatClient().lanchFrame();	}	public void lanchFrame() {		this.setLocation(400, 400);		this.setSize(300, 300);		add(tftext, BorderLayout.SOUTH);		add(taContent, BorderLayout.NORTH);		pack();		this.addWindowListener(new WindowAdapter() {			@Override			public void windowClosing(WindowEvent e) {				// TODO Auto-generated method stub				System.exit(0);			}		});		setVisible(true);	}}

相对Chat0.2,Chat0.3只多了一个点“X”就可以关闭的功能。不再上图。

Chat0.4

import java.awt.BorderLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class ChatClient extends Frame {	TextField tftext = new TextField();	TextArea taContent = new TextArea();	public static void main(String[] args) {		new ChatClient().lanchFrame();	}	public void lanchFrame() {		this.setLocation(400, 400);		this.setSize(300, 300);		add(tftext, BorderLayout.SOUTH);		add(taContent, BorderLayout.NORTH);		pack();		this.addWindowListener(new WindowAdapter() {			@Override			public void windowClosing(WindowEvent e) {				// TODO Auto-generated method stub				System.exit(0);			}		});		tftext.addActionListener(new TFListener());		setVisible(true);	}		private class TFListener implements ActionListener{		@Override		public void actionPerformed(ActionEvent e) {			String s=tftext.getText().trim();			taContent.setText(s);			tftext.setText("");					}			}}


Chat0.4在输入框内输入内容后按回车键可以直接发送。

Chat0.5比0.4多了一个服务端。废话不多说,直接上代码。

import java.io.IOException;import java.net.ServerSocket;public class ChatServer {		public static void main(String[] args) {		try {			ServerSocket ss=new ServerSocket(8888);		} catch (IOException e) {			e.printStackTrace();System.out.println("a client connected");		}	}}

ChatClient.java

   

import java.awt.BorderLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class ChatClient extends Frame {	TextField tftext = new TextField();	TextArea taContent = new TextArea();	public static void main(String[] args) {		new ChatClient().lanchFrame();	}	public void lanchFrame() {		this.setLocation(400, 400);		this.setSize(300, 300);		add(tftext, BorderLayout.SOUTH);		add(taContent, BorderLayout.NORTH);		pack();		this.addWindowListener(new WindowAdapter() {			@Override			public void windowClosing(WindowEvent e) {				// TODO Auto-generated method stub				System.exit(0);			}		});		tftext.addActionListener(new TFListener());		setVisible(true);	}		private class TFListener implements ActionListener{		@Override		public void actionPerformed(ActionEvent e) {			String s=tftext.getText().trim();			taContent.setText(s);			tftext.setText("");					}			}}
Chat0.6

   ChatServer.java

import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class ChatServer {		public static void main(String[] args) {		try {			ServerSocket ss=new ServerSocket(8888);			while(true){				Socket s=ss.accept();System.out.println("a client connected");			}		} catch (IOException e) {			e.printStackTrace();		}	}}

ChatClient.java

import java.awt.BorderLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;public class ChatClient extends Frame {	TextField tftext = new TextField();	TextArea taContent = new TextArea();	public static void main(String[] args) {		new ChatClient().lanchFrame();	}	public void lanchFrame() {		this.setLocation(400, 400);		this.setSize(300, 300);		add(tftext, BorderLayout.SOUTH);		add(taContent, BorderLayout.NORTH);		pack();		this.addWindowListener(new WindowAdapter() {			@Override			public void windowClosing(WindowEvent e) {				// TODO Auto-generated method stub				System.exit(0);			}		});		tftext.addActionListener(new TFListener());		setVisible(true);		connect();	}		public void connect(){		try {			Socket s=new Socket("127.0.0.1",8888);			System.out.println("connect");		} catch (UnknownHostException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		}	}	private class TFListener implements ActionListener{		@Override		public void actionPerformed(ActionEvent e) {			String s=tftext.getText().trim();			taContent.setText(s);			tftext.setText("");					}			}}


Chat0.7


ChatServer.java

import java.io.DataInputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class ChatServer {		public static void main(String[] args) {		try {			ServerSocket ss=new ServerSocket(8888);			while(true){				Socket s=ss.accept();System.out.println("a client connected");            DataInputStream dis=new DataInputStream(s.getInputStream());            String str=dis.readUTF();            dis.close();            System.out.println(str);			}		} catch (IOException e) {			e.printStackTrace();		}	}}

ChatClient.java

import java.awt.BorderLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;public class ChatClient extends Frame {	Socket s=null;	TextField tftext = new TextField();	TextArea taContent = new TextArea();	public static void main(String[] args) {		new ChatClient().lanchFrame();	}	public void lanchFrame() {		this.setLocation(400, 400);		this.setSize(300, 300);		add(tftext, BorderLayout.SOUTH);		add(taContent, BorderLayout.NORTH);		pack();		this.addWindowListener(new WindowAdapter() {			@Override			public void windowClosing(WindowEvent e) {				// TODO Auto-generated method stub				System.exit(0);			}		});		tftext.addActionListener(new TFListener());		setVisible(true);		connect();	}		public void connect(){		try {		   s=new Socket("127.0.0.1",8888);			System.out.println("connect");		} catch (UnknownHostException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		}	}	private class TFListener implements ActionListener{		@Override		public void actionPerformed(ActionEvent e) {			String str=tftext.getText().trim();			taContent.setText(str);			tftext.setText("");			try {				DataOutputStream dos=new DataOutputStream(s.getOutputStream());				dos.writeUTF(str);				dos.flush();				dos.close();			} catch (IOException e1) {				// TODO Auto-generated catch block				e1.printStackTrace();			}		}			}}
Chat0.8

ChatServer.java

import java.io.DataInputStream;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class ChatServer {		public static void main(String[] args) {		Boolean started=false;		try {			ServerSocket ss=new ServerSocket(8888);			started=true;			while(started){				 Boolean bconnect=false;				Socket s=ss.accept();System.out.println("a client connected");             bconnect=true;            DataInputStream dis=new DataInputStream(s.getInputStream());                          while(bconnect){            String str=dis.readUTF();            System.out.println(str);            }            dis.close();            			}		} catch (IOException e) {			e.printStackTrace();		}	}}

ChatClient.java

import java.awt.BorderLayout;import java.awt.Frame;import java.awt.TextArea;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.DataOutputStream;import java.io.IOException;import java.net.Socket;import java.net.UnknownHostException;public class ChatClient extends Frame {	Socket s=null;	DataOutputStream dos=null;	TextField tftext = new TextField();	TextArea taContent = new TextArea();	public static void main(String[] args) {		new ChatClient().lanchFrame();	}	public void lanchFrame() {		this.setLocation(400, 400);		this.setSize(300, 300);		add(tftext, BorderLayout.SOUTH);		add(taContent, BorderLayout.NORTH);		pack();		this.addWindowListener(new WindowAdapter() {			@Override			public void windowClosing(WindowEvent e) {				disconnect();				System.exit(0);			}		});		tftext.addActionListener(new TFListener());		setVisible(true);		connect();	}		public void connect(){		try {		   s=new Socket("127.0.0.1",8888);		   dos=new DataOutputStream(s.getOutputStream());			System.out.println("connect");		} catch (UnknownHostException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		}	}		public void disconnect(){		try {			dos.close();			s.close();		} catch (IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}	}	private class TFListener implements ActionListener{		@Override		public void actionPerformed(ActionEvent e) {			String str=tftext.getText().trim();			taContent.setText(str);			tftext.setText("");			try {								dos.writeUTF(str);				dos.flush();				//dos.close();			} catch (IOException e1) {				// TODO Auto-generated catch block				e1.printStackTrace();			}		}			}}

最终效果图



可以输入多个,Service可以接收多个。