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

📄 chatroom.java

📁 java编写聊天室
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  	   container.add(p12);
  	   container.add(p13);
  	   container.add(p14);
  	   container.add(p15);
  	   container.add(p16);
  	   container.add(p17);
  	   container.add(p18);
  	   container.add(p19);
  	   container.add(p20);
  	   
  	   //设置位置
  	   p1.setBounds(0, 360, 25, 25);
  	   p2.setBounds(34, 360, 25, 25);
  	   p3.setBounds(64, 360, 25, 25);
  	   p4.setBounds(94, 360, 25, 25);
  	   p5.setBounds(124, 360, 25, 25);
  	   
  	   p6.setBounds(4, 400, 25, 25);
  	   p7.setBounds(34, 400, 25, 25);
  	   p8.setBounds(64, 400, 25, 25);
  	   p9.setBounds(94, 400, 25, 25);
  	   p10.setBounds(124, 400, 25, 25);
  	   
  	   p11.setBounds(4, 440, 25, 25);
  	   p12.setBounds(34, 440, 25, 25);
  	   p13.setBounds(64, 440, 25, 25);
  	   p14.setBounds(94, 440, 25, 25);
  	   p15.setBounds(124, 440, 25, 25);
  	   
  	   p16.setBounds(4, 480, 25, 25);
  	   p17.setBounds(34, 480, 25, 25);
  	   p18.setBounds(64, 480, 25, 25);
  	   p19.setBounds(94, 480, 25, 25);
  	   p20.setBounds(124, 480, 25, 25);
  	   
   /*********************************************/   	
      	
      	setResizable(false);  //设置界面的属性为大小不可改变
      	setSize(800,600);     //设置界面的大小
      	setVisible(true);
      	addWindowListener(new WindowAdapter()
      	{
      		
      		public void windowClosing(WindowEvent e)
      		{
      			disconnect();
      			setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      		}
      	    }
      	  );
      	
     }
     
       // When   list   or   choice   is   updated 
       public   void  itemStateChanged(ItemEvent e)
       { 
           
    	// String str=ch1.getSelectedItem();
    	   
    	 //设置字体的样式
         String name=ch3.getSelectedItem();
         Font f=new Font(name,Font.BOLD,18);
         msg_txt.setFont(f);
         chat_txt.setFont(f);
         
         //设置字体的颜色
         String str1=ch2.getSelectedItem();
     	if (str1=="沉静黑色")
     	{ msg_txt.setForeground(Color.black);
     	  chat_txt.setForeground(Color.GRAY);
     	}
     	else if(str1=="热情红色")
     	{ msg_txt.setForeground(Color.red);
     	 chat_txt.setForeground(Color.GRAY);
     	}
     	else if(str1=="灰色轨迹")
     	   {msg_txt.setForeground(Color.GRAY);
     	    chat_txt.setForeground(Color.GRAY);
     	   }
     	else if(str1=="青春绿色")
        {msg_txt.setForeground(Color.GREEN);
         chat_txt.setForeground(Color.GREEN);
      	   }
     	else if(str1=="浪漫桃色")
     	{  msg_txt.setForeground(Color.PINK);
     	   chat_txt.setForeground(Color.PINK);
     	}
     	else if(str1=="清爽青色")
     	{   msg_txt.setForeground(Color.CYAN);
     	    chat_txt.setForeground(Color.CYAN);
     	}
     	else if(str1=="拘谨紫色")
     	{ msg_txt.setForeground(Color.MAGENTA);
     	  chat_txt.setForeground(Color.MAGENTA);
     	   }
     	
     	else
     	{ msg_txt.setForeground(Color.BLUE);
     	  chat_txt.setForeground(Color.BLUE);}
     	
       }
          
 
        //添加事件处理代码 
     	public void actionPerformed(ActionEvent e)
        {   
            // 打开文件
     		String cmd=e.getActionCommand();
     		if(cmd.equals("open")){
     		FileDialog f=new FileDialog(this,"OpenFile",FileDialog.LOAD);
     		f.show();
     		f.dispose();}
     		
     		//添加 表情 事件
     		 if(e.getSource()==p1){msg_txt.append("调皮"); }
    		 else if(e.getSource()==p2){msg_txt.append("再见");}
    		 else if(e.getSource()==p3){msg_txt.append("嘘..");}
    		 else if(e.getSource()==p4){msg_txt.append("鲜花");}
             else if(e.getSource()==p5){msg_txt.append("疑问");}
    		 else if(e.getSource()==p6){msg_txt.append("生气");}
    		 else if(e.getSource()==p7){msg_txt.append("闭嘴");}
    		 else if(e.getSource()==p8){msg_txt.append("惊奇");}
     		 else if(e.getSource()==p9){msg_txt.append("下雨");}
     		 else if(e.getSource()==p10){msg_txt.append("太阳");}
     		 else if(e.getSource()==p11){msg_txt.append("大哭");}
    		 else if(e.getSource()==p12){msg_txt.append("偷笑");}
    		 else if(e.getSource()==p13){msg_txt.append("握手");}
    		 else if(e.getSource()==p14){msg_txt.append("厉害");}
     		 else if(e.getSource()==p15){msg_txt.append("睡觉");}
     		 else if(e.getSource()==p16){msg_txt.append("汗..");}
     		 else if(e.getSource()==p17){msg_txt.append("酷!");}
    		 else if(e.getSource()==p18){msg_txt.append("可爱");}
    		 else if(e.getSource()==p19){msg_txt.append("爱情");}
    		 else if(e.getSource()==p20) msg_txt.append("敲打");
     		 
     		 
     		
     		
     		 
     		 
     		 Font f1=new Font("楷体",Font.BOLD,18);
             Font f2=new Font("楷体",Font.ITALIC,18);
             //选择黑体字
             if(e.getSource()==c1)
            	
            	    msg_txt.setFont(f1);
             //选择斜体字
             if(e.getSource()==c2)
             	
         	       msg_txt.setFont(f2);
             
             //选择私聊方式并选定一个用户
             if(e.getSource()==b1) 
     		 {
            	 keyBoard=true;
            	 
     			 if(list.getSelectedItem()==null)
     			 {
     				
     				msg_txt.setText("请输入一个用户");
     				send.disable();
     			 }
     			}
             //选择公聊方式
             if(e.getSource()==b2)
     		 {
     			keyBoard=false;
     			send.enable();
     			
     		   }
            
             
          //单击发送按钮
     		if(e.getSource()==send&&!msg_txt.getText().toString().equals(""))
     		{
     			//if(msg_txt.getT)
     			String message="";
     			String SendBuffer=null;
     			if(keyBoard)
     			{     
     				
     					message="PRIVATE:";
         				//SendBuffer=message+U_Name+":"+list.getSelectedItem()+":"+msg_txt.getText();
         				SendBuffer=message+U_Name+":"+list.getSelectedItem()+":"+send_Changed();
     				
     			}
     			else
     			 {
     				message="PUBLIC:";
     				//SendBuffer=message+U_Name+":"+ch1.getSelectedItem()+"地说:"+msg_txt.getText();
     				SendBuffer=message+U_Name+":"+ch1.getSelectedItem()+"地说:"+send_Changed();
     			      }
     				msg_txt.setText("");
     				writer.println(SendBuffer);
     				}
     			}
    
     	
        	
     	//线程运行方法
     	public void run()                    
     	  {
     	     while(true)
     	        {
     	           String s;
     	           //读取从服务器传来的消息
     	            try
     	               { 
     	                 s=reader.readLine();
     	                
     	               
     	                if(s!=null)
     	                {   //用StringTokenizer类来实现读取分段字符
     	                	st=new StringTokenizer(s,":");
     	                	String key=st.nextToken();
     	                	if(key.equals("PEOPLE"))
     	                	{
     	                		list.clear();
     	                		while(st.hasMoreTokens())
     	                		{
     	                			String userStr=st.nextToken();
     	                			list.addItem(userStr);
     	                			//System.out.println(userStr);
     	                		}
     	                	}
     	                	else
     	                	{
     	                		//chat_txt.append(s+"\n");
     	                		chat_txt.append(accept_Changed(s));
     	                	}
     	                	//System.out.println(s+"lyssssssss");
     	                }
     	                else
     	                {
     	                	System.out.println("kong xiao xi");
     	                	JOptionPane.showMessageDialog(null, 
     	                			"你已经和服务器断开连接!", "注意", JOptionPane.WARNING_MESSAGE);
     	                	break;
     	                }
     	               }
     	            catch(Exception e)
     	            {}
     	        }
     	  }  
    			
     	 //客户端断开连接
     	public void disconnect()
    	{
    	  if(soc!=null)
    		{
    	      try
    			  {
    				writer.println("Exit");
    				writer.flush();
    				soc.close();
    		       }
              catch(IOException e)
    			  {
    		        System.out.println("Error:"+e);
    			  }
               finally
    			{}
    	    }
    	}
     	
     	public String send_Changed()//输入换行字符处理
     	{
     		String send_string=new String();
     		StringTokenizer chang=new StringTokenizer(forbid_dirty(),"\n");
     		while(chang.hasMoreTokens())
     		{
     			String str=chang.nextToken();
     			send_string=send_string+str+"`";
     			
     		}
     		return send_string;
     	}
     	
     	public String forbid_dirty()//屏蔽脏话
     	{
     		String dirty_str=msg_txt.getText().toString();
     		dirty_str=dirty_str.replace("贱", "*");
     		dirty_str=dirty_str.replace("操", "*");
     		dirty_str=dirty_str.replace("日你妈", "***");
     		dirty_str=dirty_str.replace("日你爸", "***");
     		dirty_str=dirty_str.replace("妈逼", "**");
     		dirty_str=dirty_str.replace("傻逼", "**");
     		dirty_str=dirty_str.replace("他妈的", "***");
     		dirty_str=dirty_str.replace("她妈的", "***");
     		return dirty_str;
     	}
     	
     	public String accept_Changed(String s)//在接受到消息后对换行字符进行还原
     	{
     		String accept_string=new String();
     		StringTokenizer chang=new StringTokenizer(s,"`");
     		while(chang.hasMoreTokens())
     		{
     			String str=chang.nextToken();
     			accept_string=accept_string+str+"\n";
     			
     		}
     		return accept_string;
     	}
     	/*public static void main(String[] args)
     	{
     		Socket S=null;
     		String n=null;
     		chatRoom croom=new chatRoom(S,n);
     	}*/

  }
     	


⌨️ 快捷键说明

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