Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将STDIO重定向到java应用程序上的GUI组件?_Java_Swing_Stdio - Fatal编程技术网

如何将STDIO重定向到java应用程序上的GUI组件?

如何将STDIO重定向到java应用程序上的GUI组件?,java,swing,stdio,Java,Swing,Stdio,在C上,我将简单地创建两个管道并使用dup2覆盖std文件描述符,而在另一端,我将在无限循环上为每个输出管道(sdtout、sdterr)创建一个线程,利用管道的阻塞IO来更新适合控制台的textArea/canvas。至于stdin,我将监听此类组件上的关键事件,并将这些事件写入管道 但是我如何在Java上用swing来实现呢 我不能将本机代码混合作为项目指令。到目前为止,我已经违反了许多项目指令,所以我无法继续下去 另外,提供某种程度的终端仿真(如VT100)也很酷,但是如何告知java应用

在C上,我将简单地创建两个管道并使用dup2覆盖std文件描述符,而在另一端,我将在无限循环上为每个输出管道(sdtout、sdterr)创建一个线程,利用管道的阻塞IO来更新适合控制台的textArea/canvas。至于stdin,我将监听此类组件上的关键事件,并将这些事件写入管道

但是我如何在Java上用swing来实现呢

我不能将本机代码混合作为项目指令。到目前为止,我已经违反了许多项目指令,所以我无法继续下去

另外,提供某种程度的终端仿真(如VT100)也很酷,但是如何告知java应用程序这种功能,在unix上,我将设置术语envvar

在C上,我将执行以下操作:

#include <stdio.h>
#include <unistd.h>
#include <pthread.h>

static pthread_t workers[2];

static void *_worker(void *file)
{
    int c;
    if(!file) pthread_exit(NULL);

    while((c=fgetc(file))!=EOF) {
        // Sync and put C on the screen
    }

    pthread_exit(NULL);
}

int initConsole()
{
    int stdin_pipe[2], stdout_pipe[2], stderr_pipe[2];

    if(!(pipe(stdin_pipe)||pipe(stdout_pipe)||pipe(stderr_pipe))) {
        if(dup2(stdin_pipe[0], STDIN_FILENO)<0) return -1;
        if(dup2(stdout_pipe[1], STDOUT_FILENO)<0) return -1;
        if(dup2(stderr_pipe[1], STDERR_FILENO)<0) return -1;

        pthread_create(&workers[0], NULL, _worker, fdopen(stdout_pipe[0], "r"));
        pthread_create(&workers[1], NULL, _worker, fdopen(stderr_pipe[0], "r"));

        // Register a handler within the toolkit to push chars into the stdin_pipe

        return 0;
    }

    return -1;
}
#包括
#包括
#包括
静态pthread_t workers[2];
静态void*\u工作者(void*文件)
{
INTC;
如果(!file)pthread_退出(NULL);
而((c=fgetc(文件))!=EOF){
//同步并在屏幕上显示C
}
pthread_exit(NULL);
}
int initConsole()
{
国际标准管道[2]、标准管道[2]、标准管道[2];
如果(!(管道(标准管道)|管道(标准管道)|管道(标准管道)|管道(标准管道))){
if(dup2(stdin_pipe[0],stdin_FILENO)System.err/out和in在java中应该执行相同的操作。此外,您还继承了通道[System.inheritedChanne()]

我在第一节误解了你的问题,所以如果你需要在一个线程中读取System.in并使用EventQueue.invokeLater()添加到文本区域。

您可以通过子类化PrintStream并让类简单地写入JTextArea,将System.out定向到JTextArea。然后简单地创建类的实例并调用
System.setOut(yourInstance)


在System.in中,您可以通过对InputStream进行子类化,并通过从JTextArea返回数据来实现
read()
方法来完成类似的工作。

您不应该直接对PrintStream进行子类化,请参阅@mtraut是的,对OutputStream进行子类化可能是一个更好的主意