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

📄 chatarea.java~390~

📁 1. 消息即时通信 2. 消息发送实现一对一、一对多等多种发送模式 3. 发送的消息实现多彩文本编辑
💻 JAVA~390~
📖 第 1 页 / 共 2 页
字号:
            style.addStylesToDocument(privateDoc);
            privateDoc.insertString(privateDoc.getLength(), content + "\n",
                                    privateDoc.getStyle("regular"));

          }
          catch (BadLocationException e) {
            e.printStackTrace();
          }
        }
        else if (_message.startsWith("PUBLIC_MESSAGE:")) {
          String content = _message.substring(_message.indexOf(
              "PUBLIC_MESSAGE:") + 15);
          try {
            // publicChatDocument.insertString(publicChatDocument.getLength(),
            //                                 content + "\n", char_style);
            style.addStylesToDocument(publicDoc);
            publicDoc.insertString(publicDoc.getLength(), content + "\n",
                                   publicDoc.getStyle("regular"));
          }
          catch (BadLocationException e) {
            e.printStackTrace();
          }
        }

        else if (_message.startsWith("LOGIN_PEOPLE:")) {

          String peopleName = _message.substring(_message.indexOf(":") + 1);
          peopleTable.put(peopleName, peopleName);
          model.addElement( (String) peopleTable.get(peopleName));
          //peopleList.repaint();
          peopleList.validate();
        }

        else if (_message.startsWith("SEND_FILE:")) {
          // System.out.println(_message.substring(_message.indexOf("SEND_FILE:")+10));
          String _requestPeople = _message.substring(_message.indexOf(
              "SEND_FILE:") + 10, _message.indexOf("#TOSENDFILE#"));
          String _toSendFile = _message.substring(_message.indexOf(
              "#TOSENDFILE#") + 12, _message.indexOf("#IPADDRESS#"));
          String _ipAddress = _message.substring(_message.indexOf("#IPADDRESS#") +
                                                 11, _message.indexOf("#PORT#"));

          int port = (Integer.valueOf(_message.substring(_message.lastIndexOf(
              "#PORT#") + 6, _message.lastIndexOf("#FILE_LENGTH#")))).intValue();
          //int port=3620;
          String fileLength = _message.substring(_message.lastIndexOf(
              "#FILE_LENGTH#") + 13);
          //System.out.println(_requestPeople+"  "+_toSendFile);
          System.out.println(fileLength);

          File file = new File(_toSendFile);
          try {
            //privateChatDocument.insertString(privateChatDocument.getLength(),
            //_requestPeople+"要向您发送文件"+file.getName() + "\n", char_style);
            //style.addStylesToDocument(privateDoc);
            // privateDoc.insertString(privateDoc.getLength(),_requestPeople+"要向您发送文件"+file.getName(),privateDoc.getStyle("regular"));
            fb = new FileDisposeButton(_requestPeople, _toSendFile, _ipAddress,
                                       port, fileLength);
            this.saveFileDisposeButton.put(_requestPeople + _toSendFile, fb);

            style.addStylesToDocument(privateDoc, fb);
            //System.out.println(fb.fileInfo.getText());

            // StyledDocument doc=privateChat.getStyledDocument();
            //Styles.addStylesToDocument(doc,fb);
            privateDoc.insertString(privateDoc.getLength(), "  " + "\n",
                                    privateDoc.getStyle("jpanel"));
            privateChat.validate();
          }
          catch (BadLocationException e) {
            e.printStackTrace();
          }
        }
        else if (_message.startsWith("CANCEL_RECEIVED:")) {
          //System.out.println(_message);
          String _toPeople = _message.substring(_message.indexOf(
              "CANCEL_RECEIVED:") + 16, _message.indexOf("#FILENAME#"));
          String _fileName = _message.substring(_message.indexOf("#FILENAME#") +
                                                10);
          System.out.println(_toPeople + _fileName);
          String key = _toPeople + _fileName;
          File file = new File(_fileName);
          FileCancelButton fileCancelButton = (FileCancelButton)
              FileCancelButton.saveFileCancel.get(key);
          fileCancelButton.fileInfo.setText(_toPeople + "取消文件[" + file.getName() +
                                            "]的发送");
          fileCancelButton.cancelSendButton.setVisible(false);
          FileCancelButton.saveFileCancel.remove(fileCancelButton);
        }
        else if (_message.startsWith("CANCEL_SENDING:")) {
          //System.out.println(_message);
          String _toPeople = _message.substring(_message.indexOf(
              "CANCEL_SENDING:") + 15, _message.indexOf("#FILENAME#"));
          String _fileName = _message.substring(_message.indexOf("#FILENAME#") +
                                                10);
          System.out.println(_toPeople + _fileName);
          String key = _toPeople + _fileName;
          File file = new File(_fileName);
          FileDisposeButton fileDisposeButton = (FileDisposeButton)
              FileDisposeButton.saveFileDisposeButton.get(key);
          fileDisposeButton.fileInfo.setText(_toPeople + "取消文件[" + file.getName() +
                                             "]的发送");
          fileDisposeButton.buttonJPanel.setVisible(false);
          FileDisposeButton.saveFileDisposeButton.remove(fileDisposeButton);

        }
        else if (_message.startsWith("PEOPLE_LEAVE:")) {
          System.out.println(_message);
          String _peopleName = _message.substring(_message.indexOf(
              "PEOPLE_LEAVE:") + 13);
          model.removeElement(_peopleName);
          peopleList.validate();
          peopleTable.remove(_peopleName);

        }
        Thread.sleep(5);
        //}

      }
      catch (Exception e) {

      }
    }
  }

  public void stop(){
    try{
          this.socket.close();
          this.out.flush();
          this.out.close();
          this.in.close();
        }catch(Exception e1){
            e1.printStackTrace();
        }


  }






  public static void  main(String[] args){
    ChatArea chatarea=new ChatArea(new Hashtable());
    JFrame f=new JFrame();
    f.getContentPane().add(chatarea);
//    chatarea.setVisible(true);
    //f.setSize(800,700);
    f.setVisible(true);
    f.pack();

  }
  class Action implements ActionListener{
    public void actionPerformed(ActionEvent e){
      if(e.getSource()==sendMessageButton){
         String peoples="";
        try{
          String content=messageField.getText().trim();
          if(content.length()>0&&selectNameSB.length()>0){
            messageField.setText("");
            if (toPeople.contains("大家")) {
              publicChatDocument.insertString(publicChatDocument.getLength(),
                                   "您对大家说:" +content + "\n", char_style);
              out.writeUTF("PUBLIC_MESSAGE:" + "#MESSAGECONTENT#" + content);
            }
            else {
              Enumeration enum = toPeople.elements();
              while (enum.hasMoreElements()) {
                peoples = peoples + enum.nextElement() + "$";
              }
              privateChatDocument.insertString(privateChatDocument.getLength(),
                                               "您对" + selectNameSB + "说:" +
                                               content + "\n", char_style);
              out.writeUTF("PRIVATE_MESSAGE:" + "#MESSAGECONTENT#" + content +
                           "#TOPEOPLE#" + peoples);
            }
          }else{
            sendObject.setText("请选择对象");
          }
        }catch(BadLocationException bade){}
         catch(IOException e2){
          e2.printStackTrace();
        }
      }
      else if(e.getSource()==clearSendObjectButton){
        toPeople.removeAllElements();
        selectNameSB.delete(0,selectNameSB.length());
        sendObject.setText("");
      }
      else if(e.getSource()==sendFile){
        int state=chooser.showOpenDialog(null);
        file=chooser.getSelectedFile();
        if(file!=null&&state==(chooser.APPROVE_OPTION)){
         ClientSendFile clientSendFile=new ClientSendFile(file,toPeople);
        }
      }
      else if(e.getSource()==login){
       loginDialog.setVisible(true);
     }
     else if(e.getSource()==loginOut){
       if(socket!=null){
         try {
           socket.close();
           out.flush();
           out.close();
           in.close();
         }
         catch (IOException ioe) {}
       }
         System.exit(0);

     }

    }
  }

}








⌨️ 快捷键说明

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