It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center




7年前关闭。




如何使用Java Swing实现管道连接器,例如Yahoo管道。
或与此相关的Java Swing中的任何其他类型的接线。

最佳答案

这是在Java中使用PipedReaderPipedWriter的示例:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PipedReader;
import java.io.PipedWriter;
/**
 * @date 1st May, 2011
 * @author sanju.org
 *
 * To demonstrate the use of piped character streams.
 * Example program for piped character stream in java.
 *
 */

public class PipedReaderExample {

    public static void main(String args[]) throws IOException{
        PipedReader reader = new PipedReader();
        PipedWriter writer = new PipedWriter(reader);
        Thread readerThread = new Thread(new ReaderThread(writer));
        Thread writerThread = new Thread(new WriterThread(reader));
        readerThread.start();
        writerThread.start();
    }

}

class ReaderThread implements Runnable{
    PipedWriter writer;
    public ReaderThread(PipedWriter writer){
        this.writer = writer;
    }

    public void run() {
        InputStreamReader streamReader = new InputStreamReader(System.in);
        BufferedReader bufferedReader = new BufferedReader(streamReader);
        try {
            while (true) {
                //sample implementation reading from console
                //real implementation can be reading from a socket or a file
                //or from server side code
                String line = bufferedReader.readLine();
                writer.write(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

class WriterThread implements Runnable{
    PipedReader reader;
    public WriterThread(PipedReader reader){
        this.reader = reader;
    }

    public void run() {
        while(true){
            try {
                char c;
                while( (c = (char)reader.read()) != -1){
                    //write your business logic here
                    //could be writing into a file
                    //could be processing the date
                    System.out.println(c);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

至于将yahoo与PipedReader/Writer类一起使用,我想您已经阅读了它们的API以连接到url等

以下是与PipedReaderPipedWriter(教程和信息的混合)有关的其他链接的列表,这些链接可能会有所帮助:
  • Java IO: Pipes
  • Java IO: PipedReader
  • PipedReader PipedWriter example(JavaCodeGeeks示例)
  • PipedReader and PipedWriter and thread(Java2s示例)

  • 至于Swing方面,要显示从管道接收的数据,您将需要使用TextComponent,例如以下一种:
  • How to Use Text Areas
  • How to Use Editor Panes and Text Panes

  • 每个都有一个setText(...),可让您设置其内容

    参考:
  • http://cleanjava.wordpress.com/2011/05/01/use-of-pipedreader-and-pipedwriter-in-java/
  • 10-06 07:00