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

📄 chatpanel.java

📁 一个类似QQ的在线通讯聊天软件原码,适合初学者参考学习。
💻 JAVA
字号:
package openicq.gui;import java.awt.*;import java.awt.event.ActionEvent;import java.util.Date;import javax.swing.*;import javax.swing.event.CaretEvent;import javax.swing.event.CaretListener;import openicq.Start;import openicq.data.Resource;import org.javalib.document.RTFDocument;import org.javalib.gui.SpringToolkit;import JOscarLib.Tool.OscarInterface;/** * The <code>ChatPanel</code> class is the basic panel for writing new * messages. * @author Hansgeorg Schwibbe * @copyright 2004 */public class ChatPanel extends JPanel{  private ChatFrame chatFrame;  private JPanel jPanelHistory = new JPanel();  private JPanel jPanelMessage = new JPanel();  private JPanel jPanelButton = new JPanel();  private JScrollPane jScrollPaneHistory = new JScrollPane();  private JScrollPane jScrollPaneMessage = new JScrollPane();  private RTFDocument doc = new RTFDocument();  private JTextPane jTextPaneHistory = new JTextPane();  protected JTextArea jTextAreaMessage = new JTextArea();  protected JLabel jLabelMessage = new JLabel();  protected JLabel jLabelHistory = new JLabel();  protected JButton jButtonSend = new JButton();  protected JButton jButtonClose = new JButton();  /**   * Initializes a new instance of the class <code>ChatPanel</code>.   * @param parent the parent frame   */  public ChatPanel(JFrame parent)  {    this.chatFrame = (ChatFrame) parent;    // History panel    jPanelHistory.setPreferredSize(new Dimension(420, 162));    jPanelHistory.setLayout(new BorderLayout());    jLabelHistory.setIcon(new ImageIcon(Resource.ICON_SOURCE_TYPES[0]));    jTextPaneHistory.setContentType("text/rtf");    jTextPaneHistory.setEditable(false);    jScrollPaneHistory.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);    jScrollPaneHistory.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);    jScrollPaneHistory.getViewport().add(jTextPaneHistory, null);    jPanelHistory.add(jLabelHistory, BorderLayout.NORTH);    jPanelHistory.add(jScrollPaneHistory, BorderLayout.CENTER);    // Message panel    jPanelMessage.setPreferredSize(new Dimension(420, 92));    jPanelMessage.setLayout(new BorderLayout());    jLabelMessage.setIcon(new ImageIcon(Resource.ICON_SOURCE_TYPES[1]));    jTextAreaMessage.setLineWrap(true);    jTextAreaMessage.setForeground(Color.BLUE);    jTextAreaMessage.addCaretListener(new CaretListener()    {      public void caretUpdate(CaretEvent e)      {        action_updateInputStatus();      }    });    jScrollPaneMessage.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);    jScrollPaneMessage.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);    jScrollPaneMessage.getViewport().add(jTextAreaMessage, null);    jPanelMessage.add(jLabelMessage, BorderLayout.NORTH);    jPanelMessage.add(jScrollPaneMessage, BorderLayout.CENTER);    // Button panel    jPanelButton.setPreferredSize(new Dimension(420, 35));    jPanelButton.setLayout(new FlowLayout());    jButtonSend.setEnabled(false);    jButtonSend.addActionListener(new java.awt.event.ActionListener()    {      public void actionPerformed(ActionEvent e)      {        action_sendMessage();      }    });    jButtonSend.setFocusable(false);    jButtonClose.addActionListener(new java.awt.event.ActionListener()    {      public void actionPerformed(ActionEvent e)      {        action_closeWindow();      }    });    jButtonClose.setFocusable(false);    jPanelButton.add(jButtonSend, null);    jPanelButton.add(jButtonClose, null);    // This panel    this.setLayout(new SpringLayout());    this.add(jPanelHistory);    this.add(jPanelMessage);    this.add(jPanelButton);    SpringToolkit.makeCompactGrid(this, 3, 1, 0, 0, 0, 0);  }  /**   * Action 'Send message'.   */  protected void action_sendMessage()  {    jTextAreaMessage.requestFocus();    StringBuffer message = new StringBuffer(jTextAreaMessage.getText());    // Filter the message    while (message.length() > 0 && message.charAt(0) < 33)    {      message.deleteCharAt(0);    }    while (message.length() > 0 && message.charAt(message.length() - 1) < 33)    {      message.deleteCharAt(message.length() - 1);    }    // Send message    synchronized (Start.env)    {      if (message.length() > 0 && Start.env.getConnection() != null          && Start.env.getConnection().isLogged())      {        OscarInterface.sendMessage(Start.env.getConnection(),                                   chatFrame.getContactId(), message.toString());        Date date = new Date();        this.addMessage(((String[]) chatFrame.localized.get("chat"))[4] + " ("                        + date.toString() + "):", message.toString() + "\n",                        RTFDocument.BLUE);        jTextAreaMessage.setText("");      }      else      {        String msg = ((String[]) chatFrame.localized.get("errorMessages"))[15]                     + "\n"                     + ((String[]) chatFrame.localized.get("errorMessages"))[1];        String title = ((String[]) chatFrame.localized.get("messageTitles"))[0];        JOptionPane.showMessageDialog(chatFrame, msg, title,                                      JOptionPane.ERROR_MESSAGE);      }    }  }  /**   * Action 'Update the input status'.   */  protected void action_updateInputStatus()  {    StringBuffer message = new StringBuffer(jTextAreaMessage.getText());    // Filter the message    while (message.length() > 0 && message.charAt(0) < 33)    {      message.deleteCharAt(0);    }    while (message.length() > 0 && message.charAt(message.length() - 1) < 33)    {      message.deleteCharAt(message.length() - 1);    }    // Show the status    if (message.length() > 0)    {      jButtonSend.setEnabled(true);      jLabelMessage.setText(((String[]) chatFrame.localized.get("chat"))[1]                            + " (" + message.length() + " "                            + ((String[]) chatFrame.localized.get("chat"))[2]                            + ")");    }    else    {      jButtonSend.setEnabled(false);      jLabelMessage.setText(((String[]) chatFrame.localized.get("chat"))[1]);    }  }  /**   * Aktion 'Close the window'.   */  protected void action_closeWindow()  {    chatFrame.setVisible(false);    jTextAreaMessage.setText("");  }  /**   * Appends a new chat message to the end of the history.   * @param head the header of the message   * @param message the message   * @param color the color of the message   */  protected void addMessage(String head, String message, int color)  {    doc.println(head, 2, new int[]    {     color,     RTFDocument.BOLD    });    doc.println(message, 2, new int[]    { color    });    jTextPaneHistory.setText(doc.toString());    int length = jTextPaneHistory.getDocument().getLength();    jTextPaneHistory.setCaretPosition(length);  }}

⌨️ 快捷键说明

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