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

📄 chatboard.java

📁 java网络编程技术内幕程序代码
💻 JAVA
字号:
/* * Java Network Programming, Second Edition * Merlin Hughes, Michael Shoffner, Derek Hamner * Manning Publications Company; ISBN 188477749X * * http://nitric.com/jnp/ * * Copyright (c) 1997-1999 Merlin Hughes, Michael Shoffner, Derek Hamner; * all rights reserved; see license.txt for details. */import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class Chatboard extends Panel implements Runnable, ActionListener {
  protected TextArea output;
  protected TextField input;
  protected Queue queue;
  protected Thread listener;
  
  public Chatboard () {
    setLayout (new BorderLayout ());
    add ("Center", output = new TextArea ());
    output.setEditable (false);
    add ("South", input = new TextField ());
    input.addActionListener (this);
    queue = new Queue ();
    listener = new Thread (this);
    listener.start ();
  }

  public Dimension getPreferredSize () {
    return new Dimension (200, 150);
  }

  protected MessageOutput messageOut;
  
  public void setMessageOutput (MessageOutput messageOut) {
    this.messageOut = messageOut;
  }

  public MessageOutput getMessageOutput () {
    return new QueueOutputStream (queue);
  }

  public void actionPerformed (ActionEvent event) {
    try {
      messageOut.writeUTF (input.getText ());
      messageOut.send ();
      output.append (input.getText () + "\n");
      input.setText ("");
    } catch (IOException ignored) {
    }
  }

  public void run () {
    QueueInputStream queueIn = new QueueInputStream (queue);
    try {
      while (!Thread.interrupted ()) {
        queueIn.receive ();
        String msg = queueIn.readUTF ();
        output.append ("-- " + msg + "\n");
      }
    } catch (IOException ignored) {
    }
  }
}

⌨️ 快捷键说明

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