⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatwin.java

📁 简单聊天程序
💻 JAVA
字号:
import java.io.*; import java.net.*;import java.awt.*;import java.awt.event.*;import java.util.*;class ChatWin extends Frame{	public static final int	Port=8080;	public InetAddress LocalIp;	public ServerSocket s;	public PrintWriter out;	public TextField word=new TextField();	public String nick=new String();	Vector clientThread=new Vector();	Vector clientName=new Vector();	java.awt.List		ClientName=new java.awt.List();	java.awt.List		c_ClientName=new java.awt.List();	Checkbox 	ifPrivate=new Checkbox("privately");		public ChatWin(String mode)	{		try		{			LocalIp=InetAddress.getLocalHost();		}		catch (Exception e)		{			System.exit(0);		}				if(mode.equals("Server"))		{			try			{				BuildServer();			}			catch (Exception e)			{			}		}	}	public ChatWin(String mode,String nname,String hostName,String port)	{		try		{			LocalIp=InetAddress.getLocalHost();		}		catch (Exception e)		{			System.exit(0);		}		if(mode.equals("Client"))		{			BuildClient(nname,hostName,port);		}	}	private	void BuildServer() throws Exception 	{		Panel	PanInfo=new Panel(new GridLayout(4,1,5,0));		Panel	PanClient=new Panel();		Panel	PanChat=new Panel(new GridLayout(1,1,5,10));		String	ip=new String();						ip=LocalIp.toString();		PanInfo.setBackground(Color.white);		PanInfo.add(new	Label("The Server's name and Ip:"));		PanInfo.add(new	Label(ip));		PanInfo.add(new	Label("The port used for chatting:"));		ip=String.valueOf(Port);		PanInfo.add(new	Label(ip));				PanClient.setLayout(null);		PanClient.setBackground(Color.blue);		ClientName.reshape(insets().left+10,insets().top+10,200,130);		PanClient.add(ClientName);						TextArea chatinfo=new TextArea(10,1);		PanChat.add(chatinfo);		setTitle("Chat server!");		setSize(400,500);		setBackground(Color.green);		setLayout(new GridLayout(3,1,10,0));						add(PanInfo);		add(PanClient);		add(PanChat);		addWindowListener(new WindowAction());		setVisible(true);		//		clientName.addElement(new String("All"));		try		{			s=new ServerSocket(Port);			int i=0;			while(true)			{				Socket mysocket=s.accept();				try				{					clientThread.addElement(new ServerOne(mysocket,chatinfo,this));				}				catch (Exception e)				{					mysocket.close();				}			}		}		finally		{			s.close();			}	}		private	void BuildClient(String nname,String hostName,String port)	{		Panel	PanInfo=new Panel(new GridLayout(2,2,5,0));		Panel	PanClient=new Panel(new	GridLayout(2,1,5,0));		Panel	PanChat=new Panel();		TextArea showmsg=new TextArea();		TextArea privateMsg=new TextArea();		String	ip=new String();		int		hostPort;				hostPort=Integer.parseInt(port);		c_ClientName.add("All");		c_ClientName.select(0);		ip=LocalIp.toString();		PanInfo.reshape(insets().left+10,insets().top+30,360,50);		PanInfo.setBackground(Color.yellow);		PanInfo.add(new	Label("The Client's name and Ip:"));		PanInfo.add(new	Label(ip));		PanInfo.add(new	Label("The port used :"));		ip=String.valueOf(Port);		PanInfo.add(new	Label(ip));				Button	butsend=new Button("Sent");		butsend.addMouseListener(new MouseAction());		PanClient.reshape(insets().left+10,insets().top+440,360,50);		PanClient.add(word);		PanClient.add(butsend);				PanChat.reshape(insets().left+10,insets().top+90,380,340);		PanChat.setBackground(Color.red);		PanChat.setLayout(null);		showmsg.reshape(insets().left+5,insets().top+5,280,230);		privateMsg.reshape(insets().left+5,insets().top+240,280,90);		c_ClientName.reshape(insets().left+290,insets().top+5,85,300);		ifPrivate.reshape(insets().left+290,insets().top+310,85,25);				PanChat.add(ifPrivate);		PanChat.add(privateMsg);		PanChat.add(c_ClientName);		PanChat.add(showmsg);		setTitle("Chat client!");		setSize(400,500);		setBackground(Color.green);//		setLayout(new GridLayout(3,1,10,0));		setLayout(null);				word.addKeyListener(new KeyAction());		add(PanInfo);		add(PanClient);		add(PanChat);		addWindowListener(new WindowAction());		setVisible(true);			try		{			LocalIp=InetAddress.getByName(hostName);			Socket mysocket=new Socket(LocalIp,hostPort);			out=new	PrintWriter(new	BufferedWriter(					new OutputStreamWriter(mysocket.getOutputStream())),true);			out.println(nname);			new clientListen(mysocket,showmsg,c_ClientName,privateMsg);		}		catch(Exception	e) 		{			System.exit(0);		}			}	public void addName(java.awt.List l,String name)	{		}	class MouseAction extends MouseAdapter	{		public void mouseClicked(MouseEvent e)		{			if(ifPrivate.getState()==false)			{				out.println("@@"+c_ClientName.getSelectedItem()+":"+word.getText());				word.setText(" ");			}			else			{				out.println("$$"+c_ClientName.getSelectedItem()+":"+word.getText());				word.setText(" ");			}		}	}		class WindowAction extends WindowAdapter	{		public void windowClosing(WindowEvent e)		{			System.exit(0);		}	}	class KeyAction extends KeyAdapter	{		public void keyTyped(KeyEvent e)		{			char in=e.getKeyChar();			if(in=='\n')			{					if(ifPrivate.getState()==false)				{				out.println("@@"+c_ClientName.getSelectedItem()+":"+word.getText());				word.setText(" ");				}				else				{					out.println("$$"+c_ClientName.getSelectedItem()+":"+word.getText());					word.setText(" ");				}			}		}	}	class m	extends	MouseAdapter	{		private	TextField f;		private	Frame	ff;		public m(TextField name,Frame ff)		{			f=name;		}		public void mouseClicked(MouseEvent e)		{			nick=f.getText();		}	}}class ServerOne	extends	Thread{	private	Socket mysocket;	private	BufferedReader in;	private	PrintWriter out;	private	TextArea Tarea;	ChatWin	p;	public	String nick=new String();		public ServerOne(Socket	s,TextArea area,ChatWin Cp)	throws Exception	{		mysocket=s;		Tarea=area;		p=Cp;		in=new BufferedReader(new InputStreamReader(mysocket.getInputStream()));		out=new	PrintWriter(new	BufferedWriter(new OutputStreamWriter(					mysocket.getOutputStream())),true);		start();	}	public void FeedBack(String s)	{		try		{			out.println(s);		}catch (Exception e) {}	}	public void run()	{		try			{			nick=in.readLine();			p.ClientName.add(nick);			for(int j=0;j!=(p.clientName).size();j++)			{				FeedBack("++"+(p.clientName).elementAt(j));			}			p.clientName.addElement(new String(nick));			for(int i=0;i!=(p.clientThread).size();i++)			{									((ServerOne)((p.clientThread).elementAt(i))).FeedBack("++"+nick);							}			while(true)			{				String str=in.readLine();								if(str.indexOf("@@")==0)				{					str="@@"+nick+" -> "+str.substring(2);					Tarea.appendText(str);					Tarea.appendText("\n");										for(int i=0;i!=(p.clientThread).size();i++)					{						((ServerOne)((p.clientThread).elementAt(i))).FeedBack(str);					}				}				else				if(str.indexOf("$$")==0)				{					String 	toName;					int		posEnd;					posEnd=str.indexOf(":");					toName=str.substring(2,posEnd);					str="$$"+nick+" -> "+str.substring(2);					Tarea.appendText(str);					Tarea.appendText("\n");					if(toName.equals("All"))					{						for(int i=0;i!=(p.clientThread).size();i++)						{							((ServerOne)((p.clientThread).elementAt(i))).FeedBack(str);						}					}					else					{											for(int i=0;i!=(p.clientThread).size();i++)						{							if(((ServerOne)((p.clientThread).elementAt(i))).nick.equals(toName))							{								((ServerOne)((p.clientThread).elementAt(i))).FeedBack(str);								break;							}						}						FeedBack(str);					}				}			}		}		catch(Exception e)		{		}		finally		{			try			{				int num=(p.clientThread).size();				p.ClientName.remove(nick);				for(int i=0;i!=num;i++)				{									if(((ServerOne)((p.clientThread).elementAt(i))).nick.equals(nick))					{						(p.clientThread).remove(i);					}					if(p.clientName.elementAt(i).equals(nick))					{						p.clientName.remove(i);					}					((ServerOne)((p.clientThread).elementAt(i))).FeedBack("--"+nick);								}				mysocket.close();			}			catch(Exception e){}		}	}}class InitChat extends Frame{	Label LabHost,LabPort,LabName;	TextField TxtHost,TxtPort,TxtName;	Button 	butok;	String 	nickname=new String(),m_HostName=new String(),			m_Port=new String();	public InitChat()	{		Draw();		}	private void Draw()	{		LabHost=new Label("Host Ip");		LabPort=new Label("Host Port");		LabName=new Label("NickName");		TxtHost=new TextField("192.168.3.125");		TxtPort=new TextField("8080");		TxtName=new TextField();		butok=new Button("ok");				Font myfont=new Font("Helvetica",Font.BOLD|Font.ITALIC,12);		setLayout(null);		setSize(330,170);		setBackground(Color.yellow);		LabHost.reshape(insets().left+20,insets().top+25,50,30);		LabHost.setFont(myfont);		TxtHost.reshape(insets().left+80,insets().top+30,70,20);		LabPort.reshape(insets().left+160,insets().top+25,70,30);		LabPort.setFont(myfont);		TxtPort.reshape(insets().left+240,insets().top+30,70,20);				LabName.reshape(insets().left+20,insets().top+70,70,40);		LabName.setFont(myfont);		TxtName.reshape(insets().left+90,insets().top+80,100,20);				butok.reshape(insets().left+200,insets().top+80,100,20);		butok.addMouseListener(new MouseAction());		add(LabHost);		add(LabPort);		add(LabName);		add(TxtHost);		add(TxtPort);		add(TxtName);		add(butok);		addWindowListener(new WindowAction());				setVisible(true);	}	class WindowAction extends WindowAdapter	{		public void windowClosing(WindowEvent e)		{			System.exit(0);		}	}	class MouseAction extends MouseAdapter	{		public void mouseClicked(MouseEvent e)		{			nickname=TxtName.getText();			m_HostName=TxtHost.getText();			m_Port=TxtPort.getText();			dispose();			new ChatWin("Client",nickname,m_HostName,m_Port);		}	}}class clientListen extends Thread{	BufferedReader in;	Socket 		mysocket;	TextArea 		mytext;	TextArea		m_PrivateArea;	java.awt.List	clientname;			public clientListen(Socket s,TextArea t,java.awt.List l,TextArea pt)	throws Exception	{		mysocket=s;		mytext=t;		clientname=l;		m_PrivateArea=pt;		in=new BufferedReader(new InputStreamReader (mysocket.getInputStream()));		start();	}	public void run()	{		try		{			while(true)			{				String str=in.readLine();				if(str.indexOf("++")==0)				{					clientname.add(str.substring(2));					continue;				}				if(str.indexOf("--")==0)				{					clientname.remove(str.substring(2));					continue;				}				if(str.indexOf("@@")==0)				{					mytext.append(str.substring(2));					mytext.append("\n");				}				if(str.indexOf("$$")==0)				{					m_PrivateArea.append(str.substring(2));					m_PrivateArea.append("\n");				}			}		}catch (Exception e){}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -