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

📄 qqclient.java

📁 可实现整个校园网内聊天
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    menubar.add(menu3);
    setMenuBar(menubar);
    item1.addActionListener(this);
    item2.addActionListener(this);
    item3.addActionListener(this);
    item4.addActionListener(this);
    item5.addActionListener(this);
    item6.addActionListener(this);
    item7.addActionListener(this);
    item8.addActionListener(this);
    item9.addActionListener(this);
    item10.addActionListener(this);
  button=new JButton("发送");
  button.setMnemonic(KeyEvent.VK_ENTER); 
  button2=new JButton("查询在线用户列表中所选用户的信息");
  button3=new JButton("清空聊天记录");
  button4=new JButton("下线");
  button5=new JButton("查询离线用户列表中所选用户的信息");
  button.addActionListener(this);
  button2.addActionListener(this);
  button3.addActionListener(this);
  button4.addActionListener(this);
  button5.addActionListener(this);
  button.setBackground(color);
  button2.setBackground(color);
  button3.setBackground(color); 
  button4.setBackground(color); 
  button5.setBackground(color); 
  label1=new Label("在线用户");
  label2=new Label("个人信息");
  label3=new Label("发送信息文本框");
  label4=new Label("接收信息和文件的用户(通过下拉菜单选择)");
  label5=new Label("离线用户");
  choice=new Choice();
  choice.addItemListener(this);
  choice2=new Choice();
  choice2.addItemListener(this);
  text=new TextArea(8,30);
  text.setText("聊天记录"+'\n');
  text.setEditable(false);
  text1=new TextField(15);
  text1.setText("用户名");
  text2=new TextField(15);
  text2.setText("学院");
  text3=new TextField(15);
  text3.setText("班级");
  text4=new TextField(15);
  text4.setText("QQ号");
  text5=new TextField(15);
  text5.setText("手机号");
  text6=new TextField(15);
  text6.setText("出生日期");
  text1.setEditable(false);
  text2.setEditable(false);
  text3.setEditable(false);
  text4.setEditable(false);
  text5.setEditable(false);
  text6.setEditable(false);
  text8=new TextField(20);
  text9=new TextField(20);
      basebox=Box.createHorizontalBox();
      boxh1=Box.createVerticalBox();
      boxh2=Box.createVerticalBox();
      boxh3=Box.createHorizontalBox();
      boxh4=Box.createHorizontalBox();
      boxh5=Box.createHorizontalBox();
      boxh6=Box.createVerticalBox();
      boxh3.add(Box.createHorizontalStrut(20));
      boxh3.add(button);
      boxh3.add(Box.createHorizontalStrut(20));
      boxh3.add(button3);
      boxh3.add(Box.createHorizontalStrut(20));
      boxh3.add(button4);
      boxh1.add(Box.createVerticalStrut(10));
      boxh4.add(label1);
      boxh4.add(Box.createHorizontalStrut(10));
      boxh4.add(label5);
      boxh1.add(boxh4);
      boxh1.add(Box.createVerticalStrut(14));
      boxh5.add(choice);
      boxh5.add(Box.createHorizontalStrut(10));
      boxh5.add(choice2);
      boxh1.add(boxh5);
      boxh1.add(Box.createVerticalStrut(4));
      boxh1.add(label4);
      boxh1.add(Box.createVerticalStrut(4));
      boxh1.add(text9);
      boxh1.add(Box.createVerticalStrut(4));
      boxh1.add(text);
      boxh1.add(Box.createVerticalStrut(8));
      boxh1.add(label3);
      boxh1.add(Box.createVerticalStrut(4));
      boxh1.add(text8);
      boxh1.add(Box.createVerticalStrut(1));
      boxh1.add(boxh3);
      boxh2.add(Box.createVerticalStrut(4));
      boxh2.add(label2);
      boxh2.add(Box.createVerticalStrut(7));
      boxh2.add(text1);
      boxh2.add(Box.createVerticalStrut(3));
      boxh2.add(text2);
      boxh2.add(Box.createVerticalStrut(3));
      boxh2.add(text3);
      boxh2.add(Box.createVerticalStrut(3));
      boxh2.add(text4);
      boxh2.add(Box.createVerticalStrut(3));
      boxh2.add(text5);
      boxh2.add(Box.createVerticalStrut(3));
      boxh2.add(text6);
      boxh6.add(button2);
      boxh6.add(Box.createVerticalStrut(3));
      boxh6.add(button5);
      boxh2.add(Box.createVerticalStrut(10));
      boxh2.add(boxh6);
      basebox.add(boxh1);
      basebox.add(boxh2);
      add(basebox);
     validate();
     addWindowListener (new WindowAdapter()
       {public void windowClosing(WindowEvent e)//关闭主界面之前向服务器发送下线通知
          { try
          {
        String nn="qqxiaxian"+','+zhujiao;
        byte buffer[]=nn.getBytes();
        data_send=new DatagramPacket(buffer,buffer.length,serveraddress,9999);
        send=new DatagramSocket();
        send.send(data_send);
        }
       catch(Exception e3) {}  
          System.exit(0);}
       }
    );
     filedialog_save.addWindowListener(new WindowAdapter()
                 {public void windowClosing(WindowEvent e)
                   {filedialog_save.setVisible(false);
                   }
                 });  
    filedialog_open.addWindowListener(new WindowAdapter()
                 {public void windowClosing(WindowEvent e)
                   {filedialog_open.setVisible(false);
                   }
                 }); 
     shuaxin=new Thread(this);//双线程,一个接收在线用户和离线用户名单,一个用来接收数据
     accept=new Thread(this);
     shuaxin.start();
     accept.start();
  }
  public void itemStateChanged(ItemEvent e)
  {if(e.getSource()==choice)
   {text9.setText(choice.getSelectedItem());}
   else if(e.getSource()==choice2)
   {text9.setText(choice2.getSelectedItem());}
  }
   public void actionPerformed(ActionEvent e)
  {if(e.getSource()==button)//发送聊天数据,如果对方为在线用户,则按聊天数据处理,否则按留言进行处理
   { boolean online=false;
    String beijiao=text9.getText();
    String shuju=text8.getText();
    for(int i=0;i<choice.getItemCount();i++)
    {choice.select(i);
     String jj=choice.getSelectedItem();
     if(beijiao.equals(jj))
     {online=true;
     }
    }
    if(online)
    {
    Date date=new Date();
    text.append("你在"+qq.format(date)+"对"+beijiao+"说:"+'\n'+shuju+'\n'+'\n');
    try
        {
        String nn="liaotianshuju"+','+zhujiao+','+beijiao+','+shuju;
        byte buffer[]=nn.getBytes();
        data_send=new DatagramPacket(buffer,buffer.length,serveraddress,9999);
        send=new DatagramSocket();
        send.send(data_send);
       }
       catch(Exception e3) {} 
       text8.setText("");
     }
     else 
     {Date date=new Date();
      text.append("你在"+QQ.format(date)+"对"+beijiao+"留言:"+'\n'+shuju+'\n'+'\n');
       try
        {
        String nn="liuyan"+','+zhujiao+','+beijiao+','+shuju+','+QQ.format(date);
        byte buffer[]=nn.getBytes();
        data_send=new DatagramPacket(buffer,buffer.length,serveraddress,9999);
        send=new DatagramSocket();
        send.send(data_send);
       }
       catch(Exception e3) {} 
       text8.setText("");
     }
       
   }
   else if(e.getSource()==button2)//查询在线用户的个人基本信息
   {String beijiao=choice.getSelectedItem();
    String mm=null;
     try
        {
        String nn="chaxungerenxinxi"+','+beijiao;
        byte buffer[]=nn.getBytes();
        data_send=new DatagramPacket(buffer,buffer.length,serveraddress,9999);
        send=new DatagramSocket();
        send.send(data_send);
       }
       catch(Exception e3) {}  
   }
    else if(e.getSource()==button5)//查询离线用户的个人基本信息
   {String beijiao=choice2.getSelectedItem();
    String mm=null;
     try
        {
        String nn="chaxungerenxinxi"+','+beijiao;
        byte buffer[]=nn.getBytes();
        data_send=new DatagramPacket(buffer,buffer.length,serveraddress,9999);
        send=new DatagramSocket();
        send.send(data_send);
       }
       catch(Exception e3) {}  
   }
   else if(e.getSource()==button3)//清空聊天记录
   {text.setText("聊天记录"+'\n');
   }
   else if(e.getSource()==button4)//向服务器端发送下线通知
   {    try
          {
        String nn="qqxiaxian"+','+zhujiao;
        byte buffer[]=nn.getBytes();
        data_send=new DatagramPacket(buffer,buffer.length,serveraddress,9999);
        send=new DatagramSocket();
        send.send(data_send);
        }
       catch(Exception e3) {}  
          System.exit(0);
   }
   else if(e.getSource()==item1)//修改密码
   {mima hh=new mima();
   }
   else if(e.getSource()==item2)//修改个人信息
   {genggaixinxi nn=new genggaixinxi();
   }
   else if(e.getSource()==item3)//向服务器端发送下线通知
   {  try
          {
        String nn="qqxiaxian"+','+zhujiao;
        byte buffer[]=nn.getBytes();
        data_send=new DatagramPacket(buffer,buffer.length,serveraddress,9999);
        send=new DatagramSocket();
        send.send(data_send);
        }
       catch(Exception e3) {}  
          System.exit(0);
   }
   else if(e.getSource()==item4)//注销计算机
   {try
     {Runtime ce=Runtime.getRuntime();
      ce.exec("shutdown -l");
     }
      catch(Exception e1) {}  
   }
   else if(e.getSource()==item5)//关闭计算机
   {try
     {Runtime ce=Runtime.getRuntime();
      ce.exec("shutdown -s -f -t 0");
     }
      catch(Exception e1) {}  
   }
   else if(e.getSource()==item6)//重启计算机
   {try
     {Runtime ce=Runtime.getRuntime();
      ce.exec("shutdown -r -f -t 0");
     }
      catch(Exception e1) {}  
   }
   else if(e.getSource()==item7)//作者信息
   { zuozhe ff=new zuozhe();
   }
   else if(e.getSource()==item8)//发送文件
   {   filedialog_open.setVisible(true);
       String q;
       boolean online=false;
       String beijiao=text9.getText();
    for(int i=0;i<choice.getItemCount();i++)
    {choice.select(i);
     String jj=choice.getSelectedItem();
     if(beijiao.equals(jj))
     {online=true;
     }
    }
    if(online)//在线用户间文件传输
    {
       String nn="filetransfer"+','+zhujiao+','+text9.getText()+',';
       if(filedialog_open.getFile()!=null)
       { try
         {File file=new File(filedialog_open.getDirectory(),filedialog_open.getFile());
          file_reader=new FileReader(file);
          String filename=filedialog_open.getFile();
          if(filename.indexOf(".txt")!=-1||filename.indexOf(".java")!=-1)//限制为.TXT和.JAVA的文本文件
          {
          nn=nn+filename+',';
          bin=new BufferedReader(file_reader);
          while((q=bin.readLine())!=null)
          {nn=nn+q+'\n';
          }
          bin.close();
          file_reader.close();
          }
          else 
          {JOptionPane.showMessageDialog(this,"只支持文本格式的传输!","提示",JOptionPane.WARNING_MESSAGE);
            return;
          }
          }
          catch(IOException e2) {}
        }
        try
        {
        byte buffer[]=nn.getBytes();
        if(buffer.length>65000)//UDP数据包最大为65536字节,所以文件的大小不能超过一个UDP数据包的大小
        {JOptionPane.showMessageDialog(this,"对不起,你传送的文件太大!","提示",JOptionPane.WARNING_MESSAGE);
            return;
        }
        else
        {
        data_send=new DatagramPacket(buffer,buffer.length,serveraddress,9999);
        send=new DatagramSocket();
        send.send(data_send);
        JOptionPane.showMessageDialog(this,"发送文件成功!","提示",JOptionPane.WARNING_MESSAGE);
        }
       }
       catch(Exception e3) {} 
     }
     else //发送离线文件
     { String nn="lixianwenjian"+','+zhujiao+','+text9.getText()+',';
       if(filedialog_open.getFile()!=null)
       { try
         {File file=new File(filedialog_open.getDirectory(),filedialog_open.getFile());
          file_reader=new FileReader(file);
          String filename=filedialog_open.getFile();
          if(filename.indexOf(".txt")!=-1||filename.indexOf(".java")!=-1)

⌨️ 快捷键说明

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