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

📄 chatarea.java~4~

📁 1. 消息即时通信 2. 消息发送实现一对一、一对多等多种发送模式 3. 发送的消息实现多彩文本编辑
💻 JAVA~4~
📖 第 1 页 / 共 2 页
字号:
          catch (BadLocationException e) {
           // e.printStackTrace();
          }
        }
        else if (_message.startsWith("PUBLIC_MESSAGE:")) {
          String _headMessage=_message.substring(_message.indexOf("PUBLIC_MESSAGE")+15,_message.indexOf("#CONTENT#"));
          String content = _message.substring(_message.indexOf(
              "#CONTENT#") +9,_message.lastIndexOf("#FONTATTRIBUTE#"));
          String fontMessage=_message.substring(_message.lastIndexOf("#FONTATTRIBUTE#")+15);
          String fontFamily=fontMessage.substring(0,fontMessage.indexOf("$"));
          //String fontStyle=font.substring(font.indexOf("$")+1,font.lastIndexOf("$"));
          //String fontSize=font.substring(font.lastIndexOf("$")+1);
          int fontStyle=Integer.parseInt(fontMessage.substring(fontMessage.indexOf("$")+1,fontMessage.lastIndexOf("$")));
          int fontSize=Integer.parseInt(fontMessage.substring(fontMessage.lastIndexOf("$")+1));

          //System.out.println(fontFamily+" "+fontStyle+" "+fontSize);
          Font font=new Font(fontFamily,fontStyle,fontSize);


          try {
            // publicChatDocument.insertString(publicChatDocument.getLength(),
            //                                 content + "\n", char_style);
            style.addStylesToDocument(publicDoc,font);
            publicDoc.insertString(publicDoc.getLength(),_headMessage,
                                   publicDoc.getStyle("regular"));
            publicDoc.insertString(publicDoc.getLength(), content + "\n",
                                   publicDoc.getStyle("newStyle"));

          }
          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 {
            fb = new FileDisposeButton(_requestPeople, _toSendFile, _ipAddress,
                                       port, fileLength);
            this.saveFileDisposeButton.put(_requestPeople + _toSendFile, fb);
            style.addStylesToDocument(privateDoc, 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);
          publicDoc.insertString(publicDoc.getLength(), _peopleName + "离开\n",
                                   publicDoc.getStyle("regular"));
          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||e.getSource()==messageField){
         String peoples="";
        try{
          String content=messageField.getText().trim();
          if(content.length()>0&&selectNameSB.length()>0){
            messageField.setText("");
            messageField.setFocusable(true);
            if (toPeople.contains("大家")) {
              //publicChatDocument.insertString(publicChatDocument.getLength(),
              // "您对大家说:"+content + "\n", char_style);
              style.addStylesToDocument(publicDoc, font,color,underLine,strikeThrough,superscript,subscript);
              publicDoc.insertString(publicDoc.getLength(), "您对大家说:",
                                     publicDoc.getStyle("regular"));
              publicDoc.insertString(publicDoc.getLength(), content + "\n",
                                     publicDoc.getStyle("newStyle"));
              out.writeUTF("PUBLIC_MESSAGE:" + "#MESSAGECONTENT#" + content +
                           "#FONTATTRIBUTE#" + font.getFontName() + "$" +
                           font.getStyle() + "$" + font.getSize());
            }

            else {
              Enumeration enum = toPeople.elements();
              while (enum.hasMoreElements()) {
                peoples = peoples + enum.nextElement() + "$";
              }
              // privateChatDocument.insertString(privateChatDocument.getLength(),
              //                                "您对" + selectNameSB + "说:" +
              //                              content + "\n", char_style);
              style.addStylesToDocument(privateDoc, font,color,underLine,strikeThrough,superscript,subscript);
              // System.out.println(font.getName()+"  "+font.getSize()+"  "+font.getStyle());
              privateDoc.insertString(privateDoc.getLength(),
                                      "您对" + selectNameSB + "说:",
                                      privateDoc.getStyle("regular"));
              privateDoc.insertString(privateDoc.getLength(), content + "\n",
                                      privateDoc.getStyle("newStyle"));
              out.writeUTF("PRIVATE_MESSAGE:" +"#TOPEOPLE#" + peoples+ "#MESSAGECONTENT#" + content +
                             "#FONTATTRIBUTE#" +
                           font.getFontName() + "$" +
                           font.getStyle() + "$" + font.getSize());
            }

          }else{
            if(selectNameSB.length()<=0){
              JOptionPane.showMessageDialog(null, "请选择发送对像", "提示信息",
                                            JOptionPane.WARNING_MESSAGE);
            }else{
              JOptionPane.showMessageDialog(null, "发送消息不能为空", "提示信息",
                                            JOptionPane.WARNING_MESSAGE);
            }
            //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){
        if (selectNameSB.length() <= 0) {
          JOptionPane.showMessageDialog(null, "请先选择发送对像", "提示信息",
                                        JOptionPane.WARNING_MESSAGE);
        }else if (toPeople.contains("大家")||toPeople.contains(userName)) {
          JOptionPane.showMessageDialog(null, "发送对象不允许包含大家或者自己", "提示信息",
                                        JOptionPane.WARNING_MESSAGE);
        }else {
          int state = chooser.showOpenDialog(null);
          file = chooser.getSelectedFile();
          if (file != null && state == (chooser.APPROVE_OPTION)) {
            //if (toPeople.contains("大家")) {
            // JOptionPane.showMessageDialog(null, "发送对象不允许包含大家", "提示信息",
            ///                                 JOptionPane.WARNING_MESSAGE);
            //}else{
            ClientSendFile clientSendFile = new ClientSendFile(file, toPeople);
            // }
          }
        }
      }
      else if(e.getSource()==login){
       loginDialog.setVisible(true);
     }
     else if(e.getSource()==loginOut){
       try{
         //System.out.println(privateDoc.getText(0, privateDoc.getLength()));
       }catch(Exception e2){

       }
       if(socket!=null){
         try {
           socket.close();
           out.flush();
           out.close();
           in.close();
         }
         catch (IOException ioe) {}
       }
         System.exit(0);

     }

    }
  }
  public static void centerOnScreen(Window window) {
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
    window.setLocation( (d.width - window.getWidth()) / 2,
                       (d.height - window.getHeight()) / 2);
  }



}








⌨️ 快捷键说明

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