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

📄 client.java

📁 一个自己用JAVA写的聊天程序,希望对大家游泳
💻 JAVA
字号:
package liaotian;/* * Client.java * * Created on 2006年11月12日, 上午12:29 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. *//** * * @author xingwang *///package myjava;/* * Client.java * * Created on 2006年11月12日, 上午12:29 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. *//** * * @author xingwang */import java.net.*;import java.io.*;import java.awt.event.*;import javax.swing.*;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import java.awt.Dimension;//import lxw.*;public class Client extends JDialog implements ActionListener{	Socket sock;	String userinfo = "";	JTextArea receive = new JTextArea(7,20);	DefaultListModel lItems = new DefaultListModel();	JList online = new JList(lItems);	JTextArea sendmessage = new JTextArea(2,25);	JButton b1 = new JButton("发送");	JRadioButton jr1 = new JRadioButton("私聊");	Dimension dm = new Dimension(10,20);	DataOutputStream out;	DataInputStream in;	int Tag = 0;	public Client(Socket sock) 	{		 try {		 this.sock = sock;		 //sock = new Socket("127.0.0.1",6000);		 OutputStream os = sock.getOutputStream();		 out = new DataOutputStream(os);		 InputStream is = sock.getInputStream();		 in = new DataInputStream(is);		 CCommunion th = new CCommunion(this);		 th.start();		 }catch(IOException ee) {		 JOptionPane.showMessageDialog(null,"连接服务器失败!");		 return;		 }		JScrollPane jsp = new JScrollPane(receive);//显示信息框;		this.getContentPane().add(jsp,"East");						JScrollPane jsp2 = new JScrollPane(online);//在线列表;		this.getContentPane().add(jsp2,"West");		online.setBorder(BorderFactory.createTitledBorder("当前在线用户"));		JScrollPane jsp3 = new JScrollPane(sendmessage);//发言框;		JPanel p2 = new JPanel();		p2.add(jsp3);		p2.add(jr1);		p2.add(b1);		this.getContentPane().add(p2,"South");		b1.addActionListener(this);		jr1.addActionListener(this);		setTitle("客户端");		setSize(500,400);		setVisible(true);		setResizable(false);		addWindowListener(new WindowAdapter() 		{			public void windowClosing(WindowEvent e) 			{				try 				{					out.writeUTF("@leave");				} 				catch(Exception ee) {}				dispose();				System.exit(0);			}		});		online.addListSelectionListener(new ListSelectionListener() 		{			public void valueChanged(ListSelectionEvent e) 			{				userinfo = (String)online.getSelectedValue();				System.out.println(userinfo+"9898");			}		});	}	public void actionPerformed(ActionEvent e) 	{		if(e.getSource() == b1) 		{			if(! sendmessage.getText().equals("")) 			{				if(Tag == 1) 				{					//System.out.println("jjjj");					if(!userinfo.equals("")) 					{						try						{							Tag = 0;							jr1.setSelected(false);							out.writeUTF("@selfmessage");							out.writeUTF(userinfo);							out.writeUTF(sendmessage.getText());							sendmessage.setText("");							userinfo = "";						} 						catch (Exception ee){}					}					else					{						JOptionPane.showMessageDialog(null,"请选择聊天对象");					}				} 				else 				{					try 					{						userinfo="";						out.writeUTF("@publicmessage");						out.writeUTF(sendmessage.getText());						sendmessage.setText("");					} 					catch(Exception ee) {}				}			} 			else 			{				JOptionPane.showMessageDialog(null,"消息内容不能为空");			}		}		else if(e.getSource() == jr1) 		{			if(Tag == 1) Tag=0;			 Tag=1;			System.out.println(Tag);		}	}	/*public static void main(String args[]) 	{		//Socket sock = new Socket("127.0.0.1",6000);		new Client();	}*/}class CCommunion extends Thread {	Client fp;	String strtag;	CCommunion(Client fp) 	{		this.fp = fp;	}	public void run() 	{		String msg = null;		String msg2 = null;		while(true) 		{			try 			{				msg = fp.in.readUTF();				if(msg.equals("bye")) 				{					fp.receive.append("服务器已经停止\n");					break;				}				if(msg.equals("message")) 				{					//fp.receive.setEditable(true);					fp.receive.append(fp.in.readUTF()+"\n");					fp.receive.setEditable(false);				}				if(msg.equals("listrefresh"))				{					fp.lItems.clear();					while(true)					{						strtag=fp.in.readUTF().trim();						if(strtag.equals("END")) break;						fp.lItems.add(0, strtag);						System.out.println(strtag+"5dd");					}					//fp.lItems.add(0, "--------------------------------");				}			} 			catch(Exception ee) {break;}		}		try 		{			fp.out.close();			fp.in.close();			fp.sock.close();		}		catch(Exception ee){}	}}

⌨️ 快捷键说明

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