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

📄 chatroom.java

📁 这是我自己写的一个聊天程序
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class chatroom extends JFrame implements ActionListener,Runnable
{
  private static JLabel[]totalu;
  private static int num;
  private String username;
  private String servername;
  private int width,height;
  private String title;
  JFrame top;
  JFrame bottom;
  JFrame menu;
  JFrame user;
  Container topc;
  Container menuc;
  Container bottomc;
  Container c;
  Container u;
  JTextArea userarea;
  JTextArea messagearea;
  JScrollPane textAreaScrollPane;
  JLabel tishi;
  JTextField mess;
  JButton send;
  JButton ying;
  JButton cancle;
  JButton login;
  JButton reg;
  Client client;
  MultiJabberServer server;
  public chatroom(int width,int height,String title,String name,String servername,int port)
  {
	  totalu=new JLabel[100];
	  num=0;
	  username=name;
	  this.servername=servername;
	  this.width=width;
	  this.title=title;
	  top=new JFrame();
	  bottom=new JFrame();
	  menu=new JFrame();
	  user=new JFrame();
	  topc=top.getContentPane();
	  bottomc=bottom.getContentPane();
	  menuc=menu.getContentPane();
	  c=this.getContentPane();
	  u=user.getContentPane();
	  topc.setLayout(new BorderLayout());
	  bottomc.setLayout(new BorderLayout());
	  menuc.setLayout(new GridLayout());
	  c.setLayout(new BorderLayout());
	  u.setLayout(new BorderLayout());
	  tishi=new JLabel("消息:");
	  mess=new JTextField();
	  send=new JButton("发送");
	  messagearea=new JTextArea();
	  messagearea.setEditable(false);
 	  textAreaScrollPane=new JScrollPane(messagearea);
      ying=new JButton("隐身");
      cancle=new JButton("退出");
      login=new JButton("登录");
      reg=new JButton("注册");
      userarea=new JTextArea(10,10);
      userarea.setBackground(new Color(180,180,180));
      userarea.setForeground(new Color(150,180,150));
      userarea.setLayout(new GridLayout(20,1));
      client=new Client(username,servername,port);
      menuc.add(ying);
      menuc.add(cancle);
      menuc.add(login);
      topc.add(textAreaScrollPane,BorderLayout.CENTER);
      topc.add(menuc,BorderLayout.NORTH);
      bottomc.add(tishi,BorderLayout.WEST);
      bottomc.add(mess,BorderLayout.CENTER);
      bottomc.add(send,BorderLayout.EAST);
      u.add(new JButton("当前在线用户"),BorderLayout.NORTH);
      u.add(userarea,BorderLayout.CENTER);
      c.add(topc,BorderLayout.CENTER);
      c.add(bottomc,BorderLayout.SOUTH);
   // c.add(menuc,BorderLayout.NORTH);
      c.add(u,BorderLayout.EAST);
      setSize(width,height);
      this.setTitle(title);
      mess.addActionListener(this);
      send.addActionListener(this);
      cancle.addActionListener(this);
      this.setDefaultCloseOperation(EXIT_ON_CLOSE);
  }
  public void actionPerformed(ActionEvent e)
  {
	  if((e.getSource()==mess) || (e.getSource()==send))
	  {
		  String message=mess.getText();
		  mess.setText("");
		  try
		  {
			  client.sendMessage(message);
		  }
		  catch(Throwable ex)
		  {
			  JOptionPane.showMessageDialog(this,"信息发送出现错误:"+ex.getMessage(),"聊天错误",JOptionPane.ERROR_MESSAGE);
		  }
	  }
	  else if(e.getSource()==cancle)
	  {
		  JOptionPane.showMessageDialog(this,"客户端将关闭!","[信息提示]",JOptionPane.CLOSED_OPTION);
		  this.disable();
	  }
  }
  public void run()
  {
	  String mess;
	  while(true)
	  {
		  try
		  {
			  mess=client.receiveMessage();
			  System.out.print(mess);
			  if(mess.equals("refreshUsers"))
			  {
				  refreshUser(client.receiveMessage(),totalu,num);
				  num++;
			  }
			  else
			  {
				  messagearea.append(mess+"\n");
			  }
		  }
		 catch(Throwable te)
		 {
			 JOptionPane.showMessageDialog(this,"服务器已关闭!","聊天错误",JOptionPane.ERROR_MESSAGE);
			 break;
		 }
	
	  textAreaScrollPane.getVerticalScrollBar().setValue(textAreaScrollPane.getVerticalScrollBar().getMaximum());
	  }	 
}
  public static void main(String [] args)
  {
  }
  public void refreshUser(String username,JLabel[]j,int k)
  {
	  for(int i=0;i<k+1;i++)
	  {
		  if(username.equals(j[i].getText()))
	     {
	        continue;
         }
          JLabel adduser=new JLabel(username);
          adduser.setForeground(new Color(100,100,200));
          userarea.add(adduser);
  
  
      }
  }
}

⌨️ 快捷键说明

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