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

📄 chatframe.java

📁 一个类似QQ的在线通讯聊天软件原码,适合初学者参考学习。
💻 JAVA
字号:
package openicq.gui;import java.awt.*;import java.awt.event.WindowEvent;import javax.swing.JFrame;import javax.swing.JPanel;import openicq.Start;import openicq.data.Resource;import org.javalib.dynamic.localize.Localizable;import org.javalib.gui.image.ImageLoader;import org.javalib.sound.MediaPlayer;import org.javalib.util.TitledList;import JOscarLib.Management.Contact;/** * The <code>ChatFrame</code> class is the basic frame for writing new * messages. * @author Hansgeorg Schwibbe * @copyright 2004 */public class ChatFrame extends JFrame implements Localizable, Runnable{  protected TitledList localized = Start.env.getLocalizedList();  protected ChatPanel chatPanel = new ChatPanel(this);  private MediaPlayer player = new MediaPlayer();  private JPanel contentPane;  private Contact contact;  private Thread blinkerThread;  private String title;  private int sleeptime = 650;  /**   * Initializes a new instance of the class <code>ChatFrame</code>.   * @param c the contact for this chat window   */  public ChatFrame(Contact c)  {    this.contact = c;    try    {      this.setIconImage(ImageLoader.loadImage(Resource.ICON_SOURCE_LOGO, this));    }    catch (Exception ex)    {      System.err.println(this.getClass().getName() + ": " + ex.toString());    }    this.setContact(c);    this.localize();    contentPane = (JPanel) this.getContentPane();    contentPane.setLayout(new BorderLayout());    contentPane.add(chatPanel, BorderLayout.CENTER);    this.setResizable(false);    this.pack();    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();    Dimension frameSize = this.getSize();    if (frameSize.height > screenSize.height)    {      frameSize.height = screenSize.height;    }    if (frameSize.width > screenSize.width)    {      frameSize.width = screenSize.width;    }    this.setLocation((screenSize.width - frameSize.width) / 2,                     (screenSize.height - frameSize.height) / 2);    chatPanel.jTextAreaMessage.requestFocus();  }  /**   * Returns the chat name of this chat window.   * @return the chat name of this chat window   */  public String getChatName()  {    return Start.env.getChatName(contact);  }  /**   * Returns the contact of this chat window.   * @return the contact of this chat window   */  public Contact getContact()  {    return this.contact;  }  /**   * Returns the contact ID of this chat window.   * @return the contact ID of this chat window   */  public String getContactId()  {    return contact.getContactId();  }  /**   * Returns the display name of this chat window.   * @return the display name of this chat window   */  public String getDisplayName()  {    return Start.env.getDisplayName(contact);  }  /**   * (non-Javadoc)   * @see org.javalib.dynamic.localize.Localizable#localize()   */  public void localize()  {    String text;    int shortkey;    localized = Start.env.getLocalizedList();    this.title = ((String[]) localized.get("chat"))[1] + "  "                 + Start.env.getDisplayName(contact);    this.setTitle(title);    this.chatPanel.jTextAreaMessage.setText("");    this.chatPanel.jLabelHistory.setText(((String[]) localized.get("chat"))[0]);    this.chatPanel.jLabelMessage.setText(((String[]) localized.get("chat"))[1]);    text = ((String[]) localized.get("stdButtons"))[5];    this.chatPanel.jButtonSend.setText(text);    shortkey = ((char[]) localized.get("stdButtonsShortkeys"))[5];    this.chatPanel.jButtonSend.setMnemonic(shortkey);    text = ((String[]) localized.get("stdButtons"))[3];    this.chatPanel.jButtonClose.setText(text);    shortkey = ((char[]) localized.get("stdButtonsShortkeys"))[3];    this.chatPanel.jButtonClose.setMnemonic(shortkey);  }  /**   * (non-Javadoc)   * @see java.awt.Window#processWindowEvent(java.awt.event.WindowEvent)   */  protected void processWindowEvent(WindowEvent e)  {    if (e.getID() == WindowEvent.WINDOW_CLOSING)    {      this.setVisible(false);      chatPanel.jTextAreaMessage.setText("");    }  }  /**   * (non-Javadoc)   * @see java.lang.Runnable#run()   */  public void run()  {    boolean firstRun = true;    while (true)    {      try      {        if (this.isFocused())        {          break;        }        // Play sound        synchronized (Start.env)        {          if (firstRun == true && Start.env.isSoundEnabled()              && Start.env.getSoundFlags()[2] == true              && !Start.env.getSoundFileNames()[2].equals(""))          {            player.playSound(Start.env.getSoundFileNames()[2]);          }        }        // Set the title        this.setTitle(title);        Thread.sleep(sleeptime);        if (this.isFocused())        {          break;        }        // Remove the title        this.setTitle("");        Thread.sleep(sleeptime);      }      catch (Exception ex)      {        System.err.println(this.getClass().getName() + ": " + ex.toString());      }      firstRun = false;    }    this.setTitle(title);  }  /**   * Sets the contact of this chat window.   * @param c the contact of this chat window   */  public void setContact(Contact c)  {    this.contact = c;    this.title = ((String[]) localized.get("chat"))[1] + "  "                 + Start.env.getDisplayName(contact);    this.setTitle(title);  }  /**   * Starts the title blinking of this chat window.   */  public void startTitleBlinking()  {    if (blinkerThread != null && blinkerThread.isAlive())    {      synchronized (Start.env)      {        if (Start.env.isSoundEnabled() && Start.env.getSoundFlags()[2] == true            && !Start.env.getSoundFileNames()[2].equals(""))        {          try          {            player.playSound(Start.env.getSoundFileNames()[2]);          }          catch (Exception ex)          {            System.err.println(this.getClass().getName() + ": " + ex.toString());          }        }      }      return;    }    blinkerThread = new Thread(this);    blinkerThread.start();  }}

⌨️ 快捷键说明

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