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

📄 client.java

📁 用java写的聊天室
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				}
				return bb;
			}
			else
			{
				JLabel bb=new JLabel((String)value,new ImageIcon(Client.class.getResource("icon1/head.jpg")),JLabel.LEFT);
				bb.setForeground(new Color(221,117,162));
				bb.setToolTipText("选择该用户就可以对他发送信息!");
				if(isSelected==true)
				{
					bb.setForeground(Color.blue);
				}
				return bb;
			}
		}
	}

	//设置背景色方法
	public void setBackcolor()
	{
		JColorChooser fc=new JColorChooser();
		Color col=fc.showDialog(Client.this,"确认(A)",Color.red);
		if(col!=null)
		{
			txtPane.setBackground(col);
		}
	}

	//按钮,输入框,右键菜单的事件处理
	class ActionEv implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource()==exitBtn)//退出按钮
			{
				exit();
				System.exit(0);
			}
			if(e.getSource()==sendBtn)//发送按钮
			{
				send();
			}
			if(e.getSource()==YSBtn)//背景色设置
			{
				setBackcolor();
			}
			if(e.getSource()==MoBtn)//恢复为默认背景色
			{
				txtPane.setBackground(new Color(132,145,174,50).brighter());
			}
			if(e.getSource()==jtf)//输入框
			{
				send();
			}
			if(e.getSource()==jcomb[2])	//颜色框
			{
				String color=(String)jcomb[2].getSelectedItem();
				if(color.equals("黑色"))StyleConstants.setForeground(sa1,Color.black);
				if(color.equals("红色"))StyleConstants.setForeground(sa1,Color.red);
				if(color.equals("橙色"))StyleConstants.setForeground(sa1,Color.orange);
				if(color.equals("蓝色"))StyleConstants.setForeground(sa1,Color.blue);
				if(color.equals("蓝绿色"))StyleConstants.setForeground(sa1,Color.cyan);
				if(color.equals("绿色"))StyleConstants.setForeground(sa1,Color.green);
				if(color.equals("红紫色"))StyleConstants.setForeground(sa1,Color.magenta);
				if(color.equals("粉红色"))StyleConstants.setForeground(sa1,Color.pink);
				if(color.equals("黄色"))StyleConstants.setForeground(sa1,Color.yellow);
			}
			if(e.getSource()==ji[2])//右键菜单的设置背景色事件
			{
				setBackcolor();
			}
			if(e.getSource()==ji[3])//右键菜单的默认背景色事件
			{
				txtPane.setBackground(new Color(132,145,174,50).brighter());
			}
		}
	}

	//发送方法
	public void send()
	{
		if(LJItem.isEnabled())
		{
			JOptionPane.showMessageDialog(Client.this,"还没有连接服务器!\n请先连接服务器!");
		}
		else
		{
			String txt=jtf.getText().trim();
			if(txt.length()>100)
			{
				JOptionPane.showMessageDialog(Client.this,"要发送的字符太长!\n  请重新输入!");
			}
			else
			{
				if(jtf.getText().trim().equals("") | jtf.getText().trim()==null)
				{
					JOptionPane.showMessageDialog(Client.this,"发送内容不能为空!");
					return;
				}
				else
				{
					try
					{
						//收集数据
						DataPack pk=new DataPack();
						pk.optype=2;
						pk.from=name;
						pk.to=(String)dcdmAllU.getSelectedItem();
						pk.biaoqing=(String)jcomb[0].getSelectedItem();
						pk.txt=jtf.getText().trim();

						pk.expr=(ImageIcon)jcomb[1].getSelectedItem();

						if(jcheckb[2].isSelected())
						{
							pk.si=true;
						}
						else
						{
							pk.si=false;
						}
						oos.writeObject(pk);
						oos.flush();

					}
					catch(Exception err)
					{
					}
					jtf.setText("");
					jcomb[0].setSelectedIndex(0);
					jcomb[1].setSelectedIndex(0);
				}
			}
		}
	}

	//退出方法
	public void exit()
	{
		DataPack pk=new DataPack();
		pk.optype=3;
		pk.from=name;
		try
		{
			oos.writeObject(pk);
			oos.flush();
		}
		catch(IOException err)
		{
			System.out.println("err1:"+err);
		}
		closeSocket();
	}

	//断开连接方法
	public void disconnect()
	{
		exit();
		closeSocket();
	 }


	//菜单事件
	class MenuEv implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource()==AboutItem)
			{
				new client.about.About(Client.this,"关于本聊天室",true);
			}
			if(e.getSource()==TCItem) //退出菜单
			{
				exit();
				System.exit(0);
			}
			if(e.getSource()==DKItem) //断开连接菜单
			{
				disconnect();
				LJItem.setEnabled(true);
				DKItem.setEnabled(false);
			}
			if(e.getSource()==LJItem) //连接菜单
			{
				name=JOptionPane.showInputDialog("请输入您的昵称:(最多八个字符)","");
				if(name==null)
				{
				}
				else if(name.equals(""))
				{
					JOptionPane.showMessageDialog(Client.this,"  昵程不能为空!\n  请重新输入");
				}
				else
				{
					while(name.length()>8 | name.length()==0)
					{
						JOptionPane.showMessageDialog(Client.this,"  昵程不符合要求!\n  请重新输入");
						name = JOptionPane.showInputDialog(Client.this,"请输入您的昵称:(最多八个字符)","输入昵称",JOptionPane.OK_CANCEL_OPTION);
					}
					serverIP =JOptionPane.showInputDialog("请输入聊天室服务器的IP地址:","127.0.0.1");
					if(serverIP==null)
					{}
					else if(serverIP.equals(""))
					{
						JOptionPane.showMessageDialog(Client.this,"  服务器的IP地址不能为空!\n  请重新输入");
					}
					else
					{
						String tempPort = JOptionPane.showInputDialog("请输入聊天室服务器的端口号:","9000");
						if(tempPort==null)
						{}
						else if(tempPort.equals(""))
						{
							JOptionPane.showMessageDialog(Client.this,"  服务器的端口号不能为空!\n  请重新输入");
						}
						else
						{
							try
							{
								serverPort=Integer.parseInt(tempPort);
							}catch(Exception err)
							{
								JOptionPane.showMessageDialog(Client.this,"输入的端口号格式不正确!");
								tempPort = JOptionPane.showInputDialog(Client.this,"请输入聊天室服务器的端口号:","输入服务器端口号",JOptionPane.OK_CANCEL_OPTION);
							}

							//连接服务器
							try
							{
								sc=new Socket(serverIP,serverPort);
								ois=new ObjectInputStream(sc.getInputStream());
								oos=new ObjectOutputStream(sc.getOutputStream());
								//连接上服务器后,把该用户的姓名传到服务器
								denglu(name);

								//把List的第一项加为“所有人”
								dl.addElement("所有人");
								dcdmAllU.addElement("所有人");
								//连接上服务器后,就取得所有用户的姓名加入list
								Vector users=getUsers();
								allUserCount=users.size();
								for(int i=0;i<users.size();i++)
								{
									String tmp=(String)users.get(i);
									//屏蔽自己
									if(!(tmp.equals(name)))
									{
										dl.addElement(tmp);
										dcdmAllU.addElement(tmp);
									}
								}

								//启动线程
								t=new RecTh();
								t.start();
								//提示经连接服务器
								JOptionPane.showMessageDialog(Client.this,"已经连接服务器!");

								//连接后显示下面的公告字符串
								DefaultStyledDocument dsd=(DefaultStyledDocument)(txtPane.getStyledDocument());
								SimpleAttributeSet sa =new SimpleAttributeSet();
								String ss="欢迎来到本聊天室!请注意文明用语!不要说反动,侮辱,不健康的话语!\n否则将被踢出本聊天室!\n您当前的用户名为:"+name+"\n";
								dsd.insertString(dsd.getLength(),ss,sa);

								//把userLab改为本用户姓名
								userLab.setText(name);
								//显示当前所有在线用户
								allUserLab.setText("当前所有在线用户  "+allUserCount+"    ");
								//菜单选项进行相应的设置
								LJItem.setEnabled(false);
								DKItem.setEnabled(true);
							}
							catch(Exception err)
							{
								System.out.println("err4:"+err);
								JOptionPane.showMessageDialog(null,"不能与服务器正常连接!\n  请稍后再试!");
							}
						}
					}
				}
			}
		}

		//得到所有用户名字的方法
		public Vector getUsers()
		{
			try
			{
				DataPack dp=(DataPack)ois.readObject();
				if(dp.optype==4)
				{
					return (Vector)dp.con;
				}
			}
			catch(Exception err){}
			return null;
		}

		//登陆的方法
		public void denglu(String name)
		{
			try
			{
				DataPack pk=new DataPack();
				pk.optype=1;
				pk.con=name;

				oos.writeObject(pk);
				oos.flush();
			}
			catch(Exception err){}
		}
	}

	//线程类
	class RecTh extends Thread
	{
		public void run()
		{
			//插入文字是用的字符属性
			DefaultStyledDocument dsd=(DefaultStyledDocument)(txtPane.getStyledDocument());
			SimpleAttributeSet sa =new SimpleAttributeSet();

			String shuo="说:";
			while(true)
			{
				try
				{
					dp=(DataPack)ois.readObject();
					Object user=dp.con;
					Object con=dp.txt;
					String from=dp.from;
					String to=dp.to;
					String biaoqing=dp.biaoqing;
					ImageIcon expr=dp.expr;

					if(dp.optype==2) //聊天
					{
						DefaultStyledDocument sd=(DefaultStyledDocument)(txtPane.getStyledDocument());
						Position pp=sd.getEndPosition();

						//si判定是否是私聊
						boolean si=dp.si;

						if(si){shuo=" 悄悄的说:";}
						else{shuo=" 说:";}

						//设置光标的位置
						txtPane.setSelectionStart(dsd.getLength());
						if(biaoqing.equals("(无)"))
						{
							String ss="*"+from +"   对  "+to+shuo+con+"\n";
							try
							{
								txtPane.insertIcon(expr);
								dsd.insertString(dsd.getLength(),ss,sa);
							}
							catch(Exception err)
							{
								System.out.println("err5:"+err);
								err.printStackTrace();
							}
						}
						else
						{
							String ss="*"+from +"   对  "+to+" "+biaoqing+":"+con+"\n";
							dsd.insertString(dsd.getLength(),ss,sa);
							txtPane.insertIcon(expr);
						}
						int i=pp.getOffset();
						String len=(String)con;
						sd.setCharacterAttributes(i-len.length()-2,i,Client.sa1,true);

					}
					if(dp.optype==3) //退出
					{
						dl.removeElement(con);
						dcdmAllU.removeElement(con);
						String ss="【公告】 "+con+"离开本聊天室!\n";
						dsd.insertString(dsd.getLength(),ss,sa);
						allUserCount-=1;
						allUserLab.setText("当前所有在线用户  "+allUserCount+"    ");
					}
					if(dp.optype==4) //新用户登陆
					{
						dl.addElement((String)user);
						dcdmAllU.addElement((String)user);
						String ss="【公告】 "+user+"进入本聊天室!"+"\n";
						dsd.insertString(dsd.getLength(),ss,sa);
						allUserCount+=1;
						allUserLab.setText("当前所有在线用户  "+allUserCount+"    ");
					}

					if(dp.optype==5) //系统广播信息
					{
						String ss="【公告】 "+con+"\n";
						dsd.insertString(dsd.getLength(),ss,sa);
					}
				}catch(Exception err)
				{
					String ss="已经和服务器断开连接!\n";
					try
					{
						dsd.insertString(dsd.getLength(),ss,sa);
						allUserLab.setText("当前所有在线用户  0    ");
						dl.removeAllElements();
						dcdmAllU.removeAllElements();
						userLab.setText("当前用户");
						DKItem.setEnabled(false);
						LJItem.setEnabled(true);
					}
					catch(Exception ee){System.out.println("err6:"+ee);}
					break;
				}
			}
		}
	}
}

⌨️ 快捷键说明

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