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

📄 myserver.java

📁 JAVA聊天室 本聊天室可以实现公聊和私聊
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  			txtIn = new JTextField(10);
  			btnOK = new JButton("提交");
  			btnCancel = new JButton("取消");
  			
  			this.setLayout(null);
  			lblInf.setBounds(10,5,150,30);
  			lblInf.setFont(fn);
  			this.add(lblInf);
  			txtIn.setBounds(10,35,200,30);
  			txtIn.setFont(fn);
  			this.add(txtIn);
  			btnOK.setBounds(10,70,80,25);
  			btnOK.setFont(fn);
  			this.add(btnOK);
  			btnCancel.setBounds(130,70,80,25);
  			btnCancel.setFont(fn);
  			this.add(btnCancel);
  			
  			btnOK.addActionListener(this);
  			btnCancel.addActionListener(this);
  			this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
  			this.setResizable(false);
  			this.setSize(220,130);
  			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			Dimension frameSize = this.getSize();
			this.setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
  			this.setVisible(true);
	  	}
	  	
	  	public void del()
	  	{
	  		try
	  		{	
	  			String strDel = txtIn.getText();	  		
		  		UserData uDel;
		  		FileInputStream fis = new FileInputStream("UserData.txt");
				ObjectInputStream oisUser = new ObjectInputStream(fis);
				Vector vDel = (Vector)oisUser.readObject();				
				int a=0;
				for(;a<vDel.size();a++)
				{
					uDel = (UserData)vDel.elementAt(a);
					if(strDel.equals(uDel.id))
					{
						vDel.remove(uDel);
						FileOutputStream fos = new FileOutputStream("userdata.txt");
						ObjectOutputStream oosUser = new ObjectOutputStream(fos);
						oosUser.writeObject(vDel);
						JOptionPane.showMessageDialog(this,"删除成功!");
						oosUser.close();
						fos.close();
						this.setVisible(false);
						break;
					}
				}
				if(a >= vDel.size() + 1)
				{
					JOptionPane.showMessageDialog(this,"没有此帐号!");
				}				
			}
			catch(Exception e)
			{
				System.out.println(e.toString());
			}
	  	}
	  	
	  	public void actionPerformed(ActionEvent et)
	  	{
	  		JButton bb = (JButton)et.getSource();
	  		if(bb.equals(btnOK))
	  		{
	  			del();
	  		}
	  		else if(bb.equals(btnCancel))
	  		{
	  			 this.setVisible(false);		
	  		}
	  	}
	}
	//***********************************************************************End
	
	
	//***************************************************************添加用户窗口
	public class AddUserDil extends JDialog implements ActionListener
	{
		GridLayout gdl;
		JButton btnOK,btnCancel;
		JLabel lblId,lblPWD,lblPWD2,lblName,lblSex,lblAge,lblPhone,lblMail;
		JTextField txtId,txtName,txtAge,txtPhone,txtMail;
		JPasswordField txtPWD,txtPWD2;
		JComboBox cmbSex;
		
		public AddUserDil(UserFrm uf)
		{
			super(uf,"注册",true);
			gdl = new GridLayout(9,2,2,2);
			this.setLayout(gdl);
			btnOK = new JButton("确定");
			btnOK.setFont(fn);
			btnCancel = new JButton("取消");
			btnCancel.setFont(fn);			
			lblId = new JLabel("登录帐号");
			lblId.setFont(fn);
			lblId.setForeground(red);
			lblPWD = new JLabel("密码");
			lblPWD.setFont(fn);
			lblPWD.setForeground(red);
			lblPWD2 = new JLabel("确认密码");
			lblPWD2.setFont(fn);
			lblPWD2.setForeground(red);
			lblName = new JLabel("呢称");
			lblName.setFont(fn);
			lblName.setForeground(red);
			lblSex = new JLabel("性别");
			lblSex.setFont(fn);
			lblAge = new JLabel("年龄");
			lblAge.setFont(fn);
			lblPhone = new JLabel("电话号码");
			lblPhone.setFont(fn);
			lblMail = new JLabel("E-Mail");
			lblMail.setFont(fn);
			txtId = new JTextField(10);
			txtId.setFont(fn);
			txtPWD = new JPasswordField(10);
			txtPWD.setFont(fn);
			txtPWD2 = new JPasswordField(10);
			txtPWD2.setFont(fn);
			txtName = new JTextField(10);
			txtName.setFont(fn);
			String cc[] = {"--","男","女"};
			cmbSex = new JComboBox(cc);
			cmbSex.setFont(fn);
			txtAge = new JTextField(2);
			txtAge.setFont(fn);
			txtPhone = new JTextField(13);
			txtPhone.setFont(fn);
			txtMail = new JTextField(10);
			txtMail.setFont(fn);
			
			this.add(lblId);		this.add(txtId);
			this.add(lblPWD);		this.add(txtPWD);
			this.add(lblPWD2);		this.add(txtPWD2);
			this.add(lblName);		this.add(txtName);
			this.add(lblSex);		this.add(cmbSex);
			this.add(lblAge);		this.add(txtAge);
			this.add(lblPhone);		this.add(txtPhone);
			this.add(lblMail);		this.add(txtMail);
			this.add(btnOK);		this.add(btnCancel);
		
			btnOK.addActionListener(this);
			btnCancel.addActionListener(this);
			this.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
			this.setSize(400,300);
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			Dimension frameSize = this.getSize();
			this.setLocation((screenSize.width - frameSize.width)/2,(screenSize.height - frameSize.height)/2);
			this.setResizable(false);
			this.setVisible(true);
		}
		
		public void consition()
		{
			String id = txtId.getText();
			String pwd = new String(txtPWD.getPassword());
			String pwd2 = new String(txtPWD2.getPassword());
			String name = txtName.getText();
			String sex = (String)cmbSex.getSelectedItem();
			String age = txtAge.getText();
			String phone = txtPhone.getText();
			String mail = txtMail.getText();
		
			while(true)
			{
				if(id.equals("")||pwd.equals("")||pwd2.equals("")||name.equals(""))
				{
					JOptionPane.showMessageDialog(this,"红色部分不能为空!");
					break;
				}
				else if(id.length() > 10)
				{
					JOptionPane.showMessageDialog(this,"帐号不能超过10个英文字母、数字或汉字!");
					break;
				}
				else if(pwd.length() > 10)
				{
					JOptionPane.showMessageDialog(this,"密码不能超过10个字符!");
					break;
				}
				else if(name.length() > 10)
				{
					JOptionPane.showMessageDialog(this,"呢称不能超过10个英文字母、数字或汉字!");
					break;
				}
				else if(pwd.equals(pwd2))
				{
					try
					{					
						FileInputStream fis = new FileInputStream("UserData.txt");
						ObjectInputStream oisUser = new ObjectInputStream(fis);
						Vector vUdd = (Vector)oisUser.readObject();
						oisUser.close();
						fis.close();
						int p = 0;
						for(;p<vUdd.size();p++)
						{
							UserData udd = (UserData)vUdd.elementAt(p);
							if(id.equals(udd.id))
							{
								JOptionPane.showMessageDialog(this,"此帐号已被注册!");
								txtPWD.setText("");
								txtPWD2.setText("");
								break;
							}
						}
						if(p >= vUdd.size())
						{
							UserData uss = new UserData(id,pwd,name,sex,age,phone,mail);
							vUdd.addElement(uss);
							FileOutputStream fos = new FileOutputStream("userdata.txt");
							ObjectOutputStream oosUser = new ObjectOutputStream(fos);
							oosUser.writeObject(vUdd);
							oosUser.close();
							fos.close();
							JOptionPane.showMessageDialog(this,"添加成功");
							this.setVisible(false);
						}
						break;
					}
					catch(Exception e)
					{
						System.out.println(e.toString());
					}				
				}
				else
				{
					JOptionPane.showMessageDialog(this,"两次密码输入不相同,请重新输入!");
					txtPWD.setText("");
					txtPWD2.setText("");
					break;
				}
			}	
		}
		
		public void actionPerformed(ActionEvent evt)
		{	
			JButton bb = (JButton)evt.getSource();
			if(bb.equals(btnOK))
			{
				consition();
			}
			else if(bb.equals(btnCancel))
			{
				this.setVisible(false);
			}
		}
	}	
	//***********************************************************************End	
	
	
	class PublicMsg extends Thread										//发送信息
	{
		int line = 0;
		ObjectOutputStream oos;
		
		public PublicMsg(ObjectOutputStream out)
		{
			oos = out;
		}
		
		public void run()
		{
			try
			{
				while(true)
				{	
					String ll = msgBox.readMsg(line);
					line++;	
					oos.writeObject(new String("PublicMsg"));
					oos.writeObject(ll + "\r\n");
					sleep(500);	
				}		
			}
			catch(Exception e)
			{
				System.out.println(e.toString());
			}
		}
	}
	
	class PrivateMsg extends Thread
	{
		ObjectOutputStream oos;
		Vector vec;
		String str;
		int line = 0;
		Socket sk;
		
		public PrivateMsg(ObjectOutputStream out,Socket s)
		{
			oos = out;
			sk = s;
		}
		
		public void run()
		{
			try
			{
				while(oos != null)
				{	
					Vector vv = msgBox.readPraUs(line);
					line++;							
					for(int i=0;i<vv.size();i++)
					{
						String st = (String)vv.elementAt(i);
						for(int k=0;k<userSkt.size();k++)
						{
							PrivateUser pu = (PrivateUser)userSkt.elementAt(k);
							if(st.equals(pu.name) && pu.socket.equals(sk))
							{
								String string = msgBox.readPraMsg();
								oos.writeObject(new String("PrivateMsg"));
								oos.writeObject(string + "\r\n");
								break;	
							}
						}
					}
				}		
			}
			catch(Exception e)
			{
				System.out.println(e.toString());
			}
		}
	}
	
	class OutputUser extends Thread										//发送在线用户
	{
		ObjectOutputStream oos;
		int users = 0;
		
		public OutputUser(ObjectOutputStream out)
		{
			oos = out;
		}
		
		public void run()
		{
			try
			{
				while(oos != null)
				{
					String ll = online.readUser(users);	
					users++;
					oos.writeObject(new String("Online"));
					oos.writeObject(ll);
				}
			}
			catch(Exception e)
			{
				System.out.println(e.toString());
			}
		}
	}
	
	class DelUser extends Thread
	{
		ObjectOutputStream oos;
		
		public DelUser(ObjectOutputStream out)
		{
			oos = out;
		}
		
		public void run()
		{
			try
			{
				while(oos != null)
				{
					Vector ver = online.readDelUser();
					oos.writeObject(new String("ReOnline"));
					oos.writeObject(ver);
				}
			}
			catch(Exception e)
			{
				System.out.println(e.toString());
			}
		}
	}
		
	public static void main(String mk[])
	{
		new MyServer();
	}
}

class Online															//储存在线人数
{
	Vector vctOnline = new Vector();
	int size = 0;
	int resize = 0;
	
	synchronized public void addUser(String ur)
	{
		vctOnline.addElement(ur);
		size++;
		resize = size;
		notifyAll();
	}
	
	synchronized public String readUser(int i)
	{
		while(i >= size)
		{
			try
			{
				wait();
			}
			catch(Exception e)
			{
				System.out.println(e.toString());
			}
		}
		String ol = (String)vctOnline.elementAt(i);
		notifyAll();
		return ol;
	}
	
	synchronized public void delUser(String us)
	{
		vctOnline.remove(us);
		size--;
		notifyAll();
	}
	
	synchronized public Vector readDelUser()
	{
		while(resize <= size)
		{
			try
			{
				wait();
			}
			catch(Exception e)
			{
				System.out.println(e.toString());
			}
		}
		notifyAll();
		return vctOnline;
	}
}

class MyMsgBox															//储存MSG
{
	Vector vct = new Vector();
	Vector vctPriU;
	String ss;
	int size = 0;
	int us = 0;
	
	synchronized public void addMsg(String msg)
	{
		vct.addElement(msg);
		size++;
		notifyAll();
	}
	
	synchronized public String readMsg(int w)
	{
		while(w >= size)
		{
			try
			{
				wait();
			}
			catch(Exception e)
			{
				System.out.println(e.toString());
			}
		}
		String rr = (String)vct.elementAt(w);
		notifyAll();
		return rr;
	}
	
	synchronized public void addPraUs(Vector ve,String m)
	{
		vctPriU = ve;
		us++;
		ss = m;
		notifyAll();
	}
	
	synchronized public Vector readPraUs(int p)
	{
		while(p >= us)
		{
			try
			{
				wait();
			}
			catch(Exception e)
			{
				System.out.println(e.toString());
			}
		}
		notifyAll();
		return vctPriU;
		
	}
	
	synchronized public String readPraMsg()
	{	
		notifyAll();
		return ss;
	}
}

⌨️ 快捷键说明

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