如何将swing.JTextArea绑定到PrintStream以接受数据 [英] How to bind swing.JTextArea to PrintStream to accept data

查看:102
本文介绍了如何将swing.JTextArea绑定到PrintStream以接受数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

客户端具有gui和额外的线程(以处理套接字输入并将其打印出来以传递类型PrintStream的对象). gui格式为new javax.swing.JTextArea().我需要传递给对象PrintStream以便写入:ClientThreadIn(PrintStream inOutput){...}.如何使用PrintStream创建/绑定gui JTextArea以接受ClientThreadIn数据形式?

Client has gui and extra thread (to process socket input and print it out to passes object of type PrintStream). The gui form has new javax.swing.JTextArea(). I need to pass to thread the object PrintStream to write to: ClientThreadIn(PrintStream inOutput){...}. How to create/bind gui JTextArea to accept data form ClientThreadIn using PrintStream?

客户:

    in = new BufferedReader(new InputStreamReader(s.getInputStream())); 
    out = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
ClientThreadIn threadIn = new ClientThreadIn(in, System.out); // client passes it's System.out to thread for writing

因此,JTextArea应该类似于控制台.它应该能够接受来自线程的数据(实际上是线程写入gui的PrintStream)... 是否有类似于JTextArea.getInputStream()的东西?

So JTextArea should be similar to console. It should be able to accept data from Thread (actually Thread writes to PrintStream of gui)... Is there something similar to JTextArea.getInputStream()?

推荐答案

一种方法是创建一个类,该类将JTextArea链接到OutputStream(称为TextAreaOutputStream),并使其扩展OutputStream.给它一个StringBuilder对象,以保存要在write(int b)覆盖中构造的String,并为其提供对要写入文本的JTextArea的引用.然后,当遇到换行符时,将String写入JTextArea,但请确保在Swing事件线程上执行 或EDT.

One way is to create a class that links the JTextArea to an OutputStream, say called TextAreaOutputStream, and have it extend OutputStream. Give it a StringBuilder object to hold the String that it is constructing in the write(int b) override, and give it a reference to the JTextArea that you want to write text to. Then when a new line character is encountered, write the String to the JTextArea, but be sure to do this on the Swing event thread or EDT.

例如:

import java.io.IOException;
import java.io.OutputStream;

import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class TextAreaOutputStream extends OutputStream {

   private final JTextArea textArea;
   private final StringBuilder sb = new StringBuilder();
   private String title;

   public TextAreaOutputStream(final JTextArea textArea, String title) {
      this.textArea = textArea;
      this.title = title;
      sb.append(title + "> ");
   }

   @Override
   public void flush() {
   }

   @Override
   public void close() {
   }

   @Override
   public void write(int b) throws IOException {

      if (b == '\r')
         return;

      if (b == '\n') {
         final String text = sb.toString() + "\n";
         SwingUtilities.invokeLater(new Runnable() {
            public void run() {
               textArea.append(text);
            }
         });
         sb.setLength(0);
         sb.append(title + "> ");

         return;
      }

      sb.append((char) b);
   }
}

然后将其包装在PrintStream对象中并​​让套接字使用它就很简单了.

Then it is trivial to wrap this in a PrintStream object and have it used by your socket.

这篇关于如何将swing.JTextArea绑定到PrintStream以接受数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆