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

📄 chatarea.java~979~

📁 1. 消息即时通信 2. 消息发送实现一对一、一对多等多种发送模式 3. 发送的消息实现多彩文本编辑
💻 JAVA~979~
📖 第 1 页 / 共 3 页
字号:
package chat;
import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.text.*;
import java.net.*;
import javax.swing.JTabbedPane;
import javax.swing.text.html.*;

/**
 *
 * <p>Title: 客户端主界面</p>
 * <p>Description:客户端主界面,包括消息编辑,消息查看,文件发送,文件接收,在线列表 </p>
 * <p>Copyright: Copyright (c) 2003</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class ChatArea extends JPanel implements Runnable{

  //声明Constant映射类对象
  Constant constant = new Constant();
  BorderLayout borderLayout1 = new BorderLayout();
  Action action = new Action();
  //自动回复菜单事件对象
  RevertAction revertAction = new RevertAction();
  //控制台菜单事件对象
  ConsoleAction consoleAction = new ConsoleAction();
  //聊天记录菜单事件对象
  ChatLogAction chatLogAction = new ChatLogAction();
  //消息编辑器
  EditorMessage messageEditor = new EditorMessage();

  TablePaneAction tablePaneAction = new TablePaneAction();
  HTMLEditorKit editorKit = new HTMLEditorKit();
  String userHome = System.getProperty("user.home");
  //消息编辑器面板背景颜色
  Color bgcolor = new Color(190, 212, 255);
  //自定义边框类对象
  OvalBorder ovalBorder = new OvalBorder(5, 5, new Color(0, 153, 255),
                                         new Color(0, 153, 255));
  //自定义样式类对象,用于文件提示信息面板样式控制
  static Styles style = new Styles();
  static Font font = new Font("新宋体", Font.PLAIN, 12);
  //自动回复控制变量
  static private int REVERTOPTION = 0;
  //自动回复信息
  static private String REVERTMESSAGE = null;
  //控制台消息
  static private int CONSOLESTATE = 0;
  //聊天记录文件对象
  File chatLogFile;
  FileInputStream chatLogIn;
  FileReader chatLogFileReader;
  BufferedReader chatLogBR;
  //图片路径
  String toolbarImagePath = "/images/toolbar/";
  ImageIcon sendButtonnormalIcon = new ImageIcon(getClass().getResource(
      toolbarImagePath + "sendmesagenormal.png"));
  ImageIcon sendButtonovalIcon = new ImageIcon(getClass().getResource(
      toolbarImagePath + "sendmessageoval.png"));
  ImageIcon publicTableIcon = new ImageIcon(getClass().getResource(
      toolbarImagePath +"publictable.png"));
  ImageIcon publicTableNew = new ImageIcon(getClass().getResource(
      toolbarImagePath + "publictablenew.gif"));
  ImageIcon privateTableIcon = new ImageIcon(getClass().getResource(
      toolbarImagePath + "privatetable.png"));
  ImageIcon privateTableNew = new ImageIcon(getClass().getResource(
      toolbarImagePath + "privatetablenew.gif"));
  ImageIcon fileTableIcon = new ImageIcon(getClass().getResource(
      toolbarImagePath +  "filetable.png"));
  ImageIcon fileTableNew = new ImageIcon(getClass().getResource(
      toolbarImagePath + "filetablenew.gif"));
  ImageIcon chatLogIcon = new ImageIcon(getClass().getResource(toolbarImagePath +
      "chatLog.png"));
  ImageIcon chatLogIconNew = new ImageIcon(getClass().getResource(
      toolbarImagePath + "chatLognew.gif"));

  //发送文件处理按钮对象
  FileDisposeButton fb;
  //登录对话框对象
  static LoginDialog loginDialog = new LoginDialog();
  //文件选择器
  final JFileChooser chooser = new JFileChooser();
  //JFontChooser fontChooser = new JFontChooser(new JFrame());

  JButton jButton1 = new JButton();
  //菜单条
  JMenuBar jmenuBar = new JMenuBar();
  JMenu loginMenu = new JMenu();
  JMenu fileMenu = new JMenu();

  JMenuItem loginOut = new JMenuItem();
  JMenuItem sendFile = new JMenuItem();

  //自动回复菜单和菜单项
  ButtonGroup revertGroup = new ButtonGroup();
  JMenu revertMenu = new JMenu();
  JCheckBoxMenuItem gongzuo = new JCheckBoxMenuItem();
  JCheckBoxMenuItem chifan = new JCheckBoxMenuItem();
  JCheckBoxMenuItem youshi = new JCheckBoxMenuItem();
  JCheckBoxMenuItem zidingyi = new JCheckBoxMenuItem();
  JCheckBoxMenuItem quxiao = new JCheckBoxMenuItem();

  //控制台菜单和菜单项
  JMenu consoleMenu = new JMenu();
  JRadioButtonMenuItem consoleMenuItem = new JRadioButtonMenuItem();


  //聊天记录菜单和菜单项
  ButtonGroup chatLogGroup = new ButtonGroup();
  JMenu chatLogMenu = new JMenu();

  JCheckBoxMenuItem chatLogPriMenuItem = new JCheckBoxMenuItem();
  JCheckBoxMenuItem chatLogPubMenuItem = new JCheckBoxMenuItem();
  JCheckBoxMenuItem chatLogCloseMenuItem = new JCheckBoxMenuItem();

  //存储文件处理按钮对象的Hashtable
  static Hashtable saveFileDisposeButton = new Hashtable();
  //公共、私人、文件提示信息组件
  static JTextPane publicChat = new JTextPane();
  static JTextPane privateChat = new JTextPane();
  static JTextPane fileInfo = new JTextPane();
  //聊天记录显示组件
  JTextArea chatLog = new JTextArea();
  static JTabbedPane tablePane = new JTabbedPane();

  //取得公共和私人消息显示组件的document对象
  static Document publicChatDocument = publicChat.getDocument();
  static Document privateChatDocument = privateChat.getDocument();
  static MutableAttributeSet char_style = new SimpleAttributeSet();

  HTMLDocument privateDoc;
  HTMLDocument publicDoc;
  static StyledDocument fileInfoDoc = fileInfo.getStyledDocument();

  //声明DefaultListModel用于用户列表动态添加数据
  DefaultListModel model = new DefaultListModel();
  JList peopleList = new JList(model);

  //声明用于放置公共、私人、文件提示信息、在线用户和聊天记录的滚动面板
  JScrollPane publicjscrollPane = new JScrollPane(publicChat);
  JScrollPane privatejscrollPane = new JScrollPane(privateChat);
  JScrollPane filejscrollPane = new JScrollPane(fileInfo);
  JScrollPane listPane = new JScrollPane(peopleList);
  JScrollPane chatLogscrollPane = new JScrollPane(chatLog);
  JSplitPane jsplitPane1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                                          publicjscrollPane, privatejscrollPane);
  //jsplitPane2劈开面板用于放置现在用户列表和tablePane
  JSplitPane jsplitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
                                          listPane, tablePane);

  JPanel jpanel1 = new JPanel();

  JPanel jpanel2 = new JPanel();
  JPanel jpanel3 = new JPanel();
  JPanel jpanel4 = new JPanel();
  JPanel jpanel5 = new JPanel();

  //发送消息按钮
  JButton sendMessageButton = new JButton(sendButtonnormalIcon);

  JButton clearSendObjectButton = new JButton();

  //发送消息或文件对象显示标签
  static JLabel sendObject = new JLabel("                                  ");


  File file = null;
  //存储在先用户列表的hashtable
  static Hashtable peopleTable = new Hashtable();
  //输入、输出流对象
  static DataOutputStream out = null;
  DataInputStream in = null;
  //连接服务器的socket对象(发送消息用)
  static Socket socket = null;
  Thread messageThread = null;

  //存储发送对象的向量对象
  Vector toPeople = new Vector();
  String selectName = "";
  //存储发送对象的Stringbuffer对象
  StringBuffer selectNameSB = new StringBuffer();

  String userName;

  public ChatArea(Hashtable peopleTable) {
    this.peopleTable = peopleTable;
    try {

      jbInit();
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  void jbInit() throws Exception {

    //设置系统默认的字体
    UIManager.put("Label.font", font);
    UIManager.put("List.font", font);
    UIManager.put("TabbedPane.font", font);
    UIManager.put("MenuBar.font", font);
    UIManager.put("Menu.font", font);
    UIManager.put("MenuItem.font", font);
    UIManager.put("PopupMenu.font", font);
    UIManager.put("CheckBoxMenuItem.font", font);
    UIManager.put("RadioButtonMenuItem.font", font);
    UIManager.put("OptionPane.messageFont", font);
    UIManager.put("OptionPane.buttonFont", font);
    Component[] components = jmenuBar.getComponents();
    for (int i = 0; i < components.length; i++) {
      ( (JMenu) components[i]).setFont(font);

    }

    //初始状态设置
    publicChat.setEditable(false);
    privateChat.setEditable(false);
    fileInfo.setEditable(false);
    listPane.setPreferredSize(new Dimension(70, 75));
    centerOnScreen(loginDialog);
    centerOnScreen(fontChooser);
    loginMenu.setEnabled(false);
    fileMenu.setEnabled(false);
    revertMenu.setEnabled(false);
    consoleMenu.setEnabled(false);
    chatLogMenu.setEnabled(false);

    sendMessageButton.setEnabled(false);
    sendMessageButton.setBackground(Color.white);
    sendMessageButton.setBorder(null);
    sendMessageButton.setRolloverIcon(sendButtonovalIcon);

    clearSendObjectButton.setEnabled(false);

    publicChat.setEditable(false);
    privateChat.setEditable(false);
    jButton1.setText("jButton1");
    loginDialog.setVisible(true);
    //设置菜单和菜单显示文本
    loginMenu.setText(constant.getLang("CA_loginMenu"));
    fileMenu.setText(constant.getLang("CA_fileMenu"));
    revertMenu.setText(constant.getLang("CA_revertMenu"));

    loginOut.setText(constant.getLang("CA_loginOut"));
    sendFile.setText(constant.getLang("CA_sendFile"));
    consoleMenu.setText(constant.getLang("CA_consoleMenu"));
    chatLogMenu.setText(constant.getLang("CA_chatLogMenu"));

    revertGroup.add(gongzuo);
    revertGroup.add(chifan);
    revertGroup.add(youshi);
    revertGroup.add(zidingyi);
    revertGroup.add(quxiao);

    chatLogGroup.add(chatLogPriMenuItem);
    chatLogGroup.add(chatLogPubMenuItem);
    chatLogGroup.add(chatLogCloseMenuItem);

    gongzuo.setText(constant.getLang("CA_gongzuo"));
    chifan.setText(constant.getLang("CA_chifan"));
    youshi.setText(constant.getLang("CA_youshi"));
    zidingyi.setText(constant.getLang("CA_zidingyi"));
    quxiao.setText(constant.getLang("CA_quxiao"));

    consoleMenuItem.setText(constant.getLang("CA_consoleMenuItem"));
    chatLogPriMenuItem.setText(constant.getLang("CA_chatLogPriMenuItem"));
    chatLogPubMenuItem.setText(constant.getLang("CA_chatLogPubMenuItem"));
    chatLogCloseMenuItem.setText(constant.getLang("CA_chatLogCloseMenuItem"));

    messageEditor.setBounds(new Rectangle(5, 5, 10, 10));
    //添加键盘事件,处理Ctrl+Enter发送消息
    messageEditor.messageTextPane.addKeyListener(new keyBoardAction());

    jmenuBar.add(loginMenu);
    jmenuBar.add(fileMenu);
    jmenuBar.add(revertMenu);
    jmenuBar.add(consoleMenu);
    jmenuBar.add(chatLogMenu);

    loginMenu.add(loginOut);
    fileMenu.add(sendFile);
    revertMenu.add(gongzuo);
    revertMenu.add(chifan);
    revertMenu.add(youshi);
    revertMenu.add(zidingyi);
    revertMenu.add(quxiao);
    consoleMenu.add(consoleMenuItem);
    chatLogMenu.add(chatLogPriMenuItem);
    chatLogMenu.add(chatLogPubMenuItem);
    chatLogMenu.add(chatLogCloseMenuItem);
    publicjscrollPane.setBorder(ovalBorder);
    publicjscrollPane.setBackground(Color.white);
    privatejscrollPane.setBorder(ovalBorder);
    chatLogscrollPane.setBorder(ovalBorder);
    privatejscrollPane.setBackground(Color.white);
    filejscrollPane.setBorder(ovalBorder);
    filejscrollPane.setBackground(new Color(140, 173, 215));

    tablePane.addTab("", publicTableIcon, publicjscrollPane);
    tablePane.addTab("", privateTableIcon, privatejscrollPane);
    tablePane.addTab("", fileTableIcon, filejscrollPane);
    tablePane.addMouseListener(tablePaneAction);

    //设置劈开面板
    jsplitPane1.setDividerSize(12);
    jsplitPane1.setOneTouchExpandable(true);
    jsplitPane2.setDividerSize(12);
    jsplitPane2.setOneTouchExpandable(true);

    clearSendObjectButton.setText("清除选定对像");

    //设置公共和私人聊天区的处理字符的编码格式
    privateChat.setContentType("text/html");

    publicChat.setContentType("text/html");


    fileInfo.setBackground(new Color(140, 173, 215));
    chatLog.setEditable(false);

    //取得公共和私聊区的 Document 对象
    privateDoc = (HTMLDocument) privateChat.getDocument();
    publicDoc = (HTMLDocument) publicChat.getDocument();

    jpanel1.setBackground(bgcolor);
    jpanel1.setBorder(new OvalBorder(15, 5, Color.black, Color.black));

⌨️ 快捷键说明

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