码迷,mamicode.com
首页 > 编程语言 > 详细

java即时在线聊天(一)

时间:2014-08-09 23:06:29      阅读:437      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   os   io   for   2014   

 

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


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);
	}

}

效果图

     bubuko.com,布布扣

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

 

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);
		}

		

}


bubuko.com,布布扣

 

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

如图 红色的小正方形                                 bubuko.com,布布扣

 

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("");
			
		}
		
	}
}


bubuko.com,布布扣

 

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();
			}
		}
		
	}
}


最终效果图

 

 

bubuko.com,布布扣

 

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

java即时在线聊天(一),布布扣,bubuko.com

java即时在线聊天(一)

标签:style   blog   http   java   os   io   for   2014   

原文地址:http://www.cnblogs.com/zuimeng/p/3901726.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!