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

📄 chatarea.java~975~

📁 1. 消息即时通信 2. 消息发送实现一对一、一对多等多种发送模式 3. 发送的消息实现多彩文本编辑
💻 JAVA~975~
📖 第 1 页 / 共 3 页
字号:

          }
        }
        catch (BadLocationException bade) {}
        catch (IOException e2) {
        }
      }
      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, constant.getLang("CA_qxxzfsdx"),
                                        constant.getLang("CA_tsxx"),
                                        JOptionPane.WARNING_MESSAGE);
        }
        else if (toPeople.contains("大家") || toPeople.contains(userName)) {//发送对象包含大家或者自己
          JOptionPane.showMessageDialog(null,
                                        constant.getLang("CA_fsdxbyxbhdjhzj"),
                                        constant.getLang("CA_tsxx"),
                                        JOptionPane.WARNING_MESSAGE);
        }
        else {
          int state = chooser.showOpenDialog(null);//显示文件选择器
          file = chooser.getSelectedFile();//取得用户选择的文件
          if (file != null && state == (chooser.APPROVE_OPTION)) {//用户选择了文件并且点击了确定
            if (file.exists()) {//文件存在
              ClientSendFile clientSendFile = new ClientSendFile(file, toPeople);
            }
            else {//文件不存在
              JOptionPane.showMessageDialog(null,
                                            constant.getLang("CA_wjbczhzsy"),
                                            constant.getLang("CA_tsxx"),
                                            JOptionPane.WARNING_MESSAGE);
            }
          }
        }
      }
      else if (e.getSource() == loginOut) {//用户退出
        try {
          int selected = JOptionPane.showConfirmDialog(ChatArea.this,
              constant.getLang("CA_bcltjlm"), constant.getLang("CA_tsxx"),
              JOptionPane.YES_NO_OPTION);//显示保存记录对话框
          if (selected == JOptionPane.YES_OPTION) {//选择确定
            String userHome = System.getProperty("user.home");//取得用户目录
            File fileDir = new File(userHome + "/聊天记录", userName);//创建保存记录文件夹
            if (!fileDir.exists()) {//文件不存在
              fileDir.mkdir();
            }
            File saveFile;
            RandomAccessFile outputStream;
            try {
              saveFile = new File(fileDir, userName + "公共");//公共聊天记录
              outputStream = new RandomAccessFile(saveFile, "rw");
              outputStream.seek(outputStream.length());
              outputStream.write( (publicDoc.getText(0, publicDoc.getLength())).
                                 getBytes());
              outputStream.close();
            }
            catch (Exception e2) {
            }
            try {
              saveFile = new File(fileDir, userName + "私人");//私人聊天记录
              outputStream = new RandomAccessFile(saveFile, "rw");
              outputStream.seek(outputStream.length());
              outputStream.write( (privateDoc.getText(0, privateDoc.getLength())).
                                 getBytes());
              outputStream.close();
            }
            catch (Exception e2) {
            }

          }
          out.writeUTF("PRIVATEMESSAGELOG:" +
                       privateDoc.getText(0, privateDoc.getLength()));//上传私人聊天记录

          Thread.sleep(5);
          out.writeUTF("PUBLICMESSAGELOG:" +
                       publicDoc.getText(0, publicDoc.getLength()));//上传公共聊天记录
        }
        catch (Exception e2) {

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

  class RevertAction
      implements ActionListener {//自动回复事件
    public void actionPerformed(ActionEvent e) {
      JMenuItem menuItem = (JMenuItem) e.getSource();
      if (menuItem == quxiao) {//取消事件
        REVERTOPTION = 0;
        REVERTMESSAGE = null;
      }
      if (menuItem == zidingyi) {//自定义事件
        String inputVaule = JOptionPane.showInputDialog(ChatArea.this,
            constant.getLang("CA_srzdyxx"));//弹出输入框
        if (inputVaule != null && inputVaule.length() > 0) {//选择确定,并且输入消息
          REVERTOPTION = 1;
          REVERTMESSAGE = inputVaule;
        }
        else {
          quxiao.setSelected(true);
          REVERTOPTION = 0;
          REVERTMESSAGE = null;
        }
      }
      else {
        REVERTOPTION = 1;
        REVERTMESSAGE = menuItem.getText();
      }
    }
  }

  class ConsoleAction
      implements ActionListener {//控制台事件
    public void actionPerformed(ActionEvent e) {
      if (e.getSource() == consoleMenuItem) {
        if (consoleMenuItem.isSelected()) {
          CONSOLESTATE = 1;//设置标志位
        }
        else {
          CONSOLESTATE = 0;
        }
      }
    }
  }
  /**
   *
   * <p>Title: 聊天记录事件内部类</p>
   * <p>Description: 处理聊天记录菜单项</p>
   * <p>Copyright: Copyright (c) 2003</p>
   * <p>Company: </p>
   * @author not attributable
   * @version 1.0
   */
  class ChatLogAction
      implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      String s;
      chatLogFile = null;
      if (e.getSource() == chatLogPriMenuItem) {//查看私人聊天记录
        chatLogFile = new File(userHome + "/聊天记录/" + userName,
                               userName + "私人");

      }
      else if (e.getSource() == chatLogPubMenuItem) {//查看公共聊天记录
        chatLogFile = new File(userHome + "/聊天记录/" + userName,
                               userName + "公共");
      }
      else if (e.getSource() == chatLogCloseMenuItem) {//关闭查看面板
        tablePane.remove(chatLogscrollPane);
        chatLog.setText("");
      }
      if (chatLogPriMenuItem.isSelected() || chatLogPubMenuItem.isSelected()) {
        if (chatLogFile.exists()) {//聊天记录存在
          try {
            tablePane.addTab("", chatLogIconNew, chatLogscrollPane);
            chatLog.setText("");
            chatLogFileReader = new FileReader(chatLogFile);
            chatLogBR = new BufferedReader(chatLogFileReader);
            while ( (s = chatLogBR.readLine()) != null) {
              chatLog.append(s + "\n");
            }
          }
          catch (Exception e1) {
            e1.printStackTrace();
          }
        }
        else {
          JOptionPane.showMessageDialog(null, constant.getLang("CA_myltjl"),
                                        constant.getLang("CA_tsdhk"),
                                        JOptionPane.WARNING_MESSAGE);
        }
      }
      else {
        tablePane.remove(chatLogscrollPane);
        chatLog.setText("");
      }
    }
  }
  /**
   *
   * <p>Title:键盘时间内部类 </p>
   * <p>Description:处理消息编辑框键盘事件Ctrl+Enter实现发送 </p>
   * <p>Copyright: Copyright (c) 2003</p>
   * <p>Company: </p>
   * @author not attributable
   * @version 1.0
   */
  class keyBoardAction
      implements KeyListener {
    public void keyPressed(KeyEvent e) {
      if (e.getKeyCode() == KeyEvent.VK_ENTER) {
        if ( (e.getModifiers() & InputEvent.CTRL_MASK) != 0) {//用户同时按下Ctrl+Enter
          String peoples = "";
          try {
            String content = messageEditor.getMessage().trim();
            if (content.length() > 0 && selectNameSB.length() > 0) {
              messageEditor.setMessage();
              messageEditor.messageTextPane.setFocusable(true);
              if (toPeople.contains("大家")) {
                StringReader stringReader = new StringReader(
                    "<p><font color=\"#0080C0\">^您对大家说:^</font></p>" + content);
                editorKit.read(stringReader, publicDoc, publicDoc.getLength());
                if (CONSOLESTATE == 1) {
                  out.writeUTF("CONSOLE_PUBLIC_MESSAGE:" + "#MESSAGECONTENT#" +
                               content);
                }
                else {
                  out.writeUTF("PUBLIC_MESSAGE:" + "#MESSAGECONTENT#" + content);
                }
              }
              else {
                Enumeration enum = toPeople.elements();
                while (enum.hasMoreElements()) {
                  peoples = peoples + enum.nextElement() + "$";
                }
                StringReader stringReader = new StringReader(
                    "<p><font color=\"#0080C0\">^您对" + selectNameSB +
                    "说:^</font></p>" + content);

                editorKit.read(stringReader, privateDoc, privateDoc.getLength());
                if (CONSOLESTATE == 1) {
                  out.writeUTF("CONSOLE_PRIVATE_MESSAGE:" + "#TOPEOPLE#" +
                               peoples + "#MESSAGECONTENT#" + content);
                }
                else {
                  out.writeUTF("PRIVATE_MESSAGE:" + "#TOPEOPLE#" + peoples +
                               "#MESSAGECONTENT#" + content);
                }
              }
            }
            else {
              if (selectNameSB.length() <= 0) {
                JOptionPane.showMessageDialog(null,
                                              constant.getLang("CA_qxxzfsdx"),
                                              constant.getLang("CA_tsxx"),
                                              JOptionPane.WARNING_MESSAGE);
              }
              else {
                JOptionPane.showMessageDialog(null,
                                              constant.getLang("CA_fsxxbnwk"),
                                              constant.getLang("CA_tsxx"),
                                              JOptionPane.WARNING_MESSAGE);
              }
            }
          }
          catch (BadLocationException bade) {}
          catch (IOException e2) {
          }

        }
      }
    }

    public void keyTyped(KeyEvent e) {

    }

    public void keyReleased(KeyEvent e) {

    }
  }
  /**
   *
   * <p>Title:table面板事件内部类 </p>
   * <p>Description: </p>
   * <p>Copyright: Copyright (c) 2003</p>
   * <p>Company: </p>
   * @author not attributable
   * @version 1.0
   */

  class TablePaneAction
      implements MouseListener {
    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
      JTabbedPane tablePane = (JTabbedPane) e.getSource();
      if (tablePane.getSelectedIndex() == 0) {
        tablePane.setIconAt(0, publicTableIcon);
      }
      if (tablePane.getSelectedIndex() == 1) {
        tablePane.setIconAt(1, privateTableIcon);
      }
      if (tablePane.getSelectedIndex() == 2) {
        tablePane.setIconAt(2, fileTableIcon);
      }
      if (tablePane.getSelectedComponent() == chatLogscrollPane) {
        tablePane.setIconAt(tablePane.getSelectedIndex(), chatLogIcon);
      }
    }
  }

  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 + -