如何制作自定义Java/JavaFX控制台? [英] How to make custom Java/JavaFX console?

查看:155
本文介绍了如何制作自定义Java/JavaFX控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有必要制作一个自定义控制台.我有以下代码:

It's necessary to make a custom console. I have the following code:

public class Console extends OutputStream{

private Console(ResourceBundle resourceBundle) throws IOException {
    FXMLLoader loader = new FXMLLoader(this.getClass().getResource("Console.fxml"), resourceBundle);
    controller = loader.getController();
    Scene scene = new Scene((Parent) loader.load());
    stage = new Stage();
    stage.setScene(scene);
    show();
}

@Override
public void write(int b) throws IOException {
    controller.append(b);
}

public static Console getInstance(ResourceBundle resourceBundle) {
    if (console == null) {
        try {
            console = new Console(resourceBundle);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
    return console;
}

public void show() {
    stage.show();
}

private static Console console = null;
private ConsoleController controller;
private Stage stage;
}

控制文件:

public class ConsoleController implements Initializable {

@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
}

@FXML
public void append(int i) {
    textArea.appendText(String.valueOf((char) i));
}

@FXML
private TextArea textArea;
}

所有这些都是从"start()"调用的:

And all these are called from the 'start()':

    @Override
public void start(Stage primaryStage) throws IOException {
    ...
    System.setErr(new PrintStream(Console.getInstance(rb)));
}

在异常期间,Console.fxmltextArea中不会打印任何内容,但是会引发以下异常:

During the exception nothing is printed in the textArea of the Console.fxml, but the following exception is thrown:

线程"JavaFX Application Thread"中的异常 异常:线程"JavaFX Application Thread"中的UncaughtExceptionHandler引发了java.lang.NullPointerException

Exception in thread "JavaFX Application Thread" Exception: java.lang.NullPointerException thrown from the UncaughtExceptionHandler in thread "JavaFX Application Thread"

我在做什么错了?

------ 编辑 --------

------EDIT--------

了解必须使用多个线程后,我将获得以下代码:

After understanding that it's necessary to use several threads I have the following code:

public class Console{
private Console(ResourceBundle resourceBundle) throws IOException {
    FXMLLoader loader = new FXMLLoader(this.getClass().getResource("Console.fxml"), resourceBundle);
    Parent root = (Parent) loader.load();
    controller = loader.getController();
    Scene scene = new Scene(root);
    stage = new Stage();
    stage.setScene(scene);
    show();

    if (errorOutputThread != null) {
        errorOutputThread.interrupt();
        try {
            errorOutputThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        errorOutputThread = null;
    }

    if (outOutputThread != null) {
        outOutputThread.interrupt();
        try {
            outOutputThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        outOutputThread = null;
    }

    System.err.flush();
    System.out.flush();

    outPipedInputStream = new PipedInputStream();
    outPipedOutputStream = new PipedOutputStream(outPipedInputStream);
    System.setOut(new PrintStream(outPipedOutputStream));

    errorPipedInputStream = new PipedInputStream();
    errorPipedOutputStream = new PipedOutputStream(errorPipedInputStream);
    System.setErr(new PrintStream(errorPipedOutputStream));

    outOutputThread = new Thread(new ConsoleStream(outPipedInputStream, "OUT"));
    outOutputThread.setDaemon(true);
    outOutputThread.start();

    errorOutputThread = new Thread(new ConsoleStream(errorPipedInputStream, "ERROR"));
    errorOutputThread.setDaemon(true);
    errorOutputThread.start();

    controller.appendText("Start Console");

}

public static Console getInstance(ResourceBundle resourceBundle) {
    if (console == null) {
        try {
            console = new Console(resourceBundle);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
    return console;
}

public void show() {
    stage.show();
}

private class ConsoleStream implements Runnable {
    private ConsoleStream(InputStream in, String type) {
        inputStream = in;
        this.type = type;
    }

    public void run() {
        try {
            InputStreamReader is = new InputStreamReader(inputStream);
            BufferedReader br = new BufferedReader(is);
            String read = null;
            read = br.readLine();
            while(read != null) {
                controller.appendText(read + "\n");
                read = br.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        controller.appendText("Thread" + type + "started");

    }

    private final InputStream inputStream;
    private String type;
}

private static Console console = null;
private ConsoleController controller;
private Stage stage;
private PrintStream printStream;
private PipedOutputStream customPipedOutputStream;
private PipedOutputStream errorPipedOutputStream;
private PipedOutputStream outPipedOutputStream;
private PipedInputStream customPipedInputStream;
private PipedInputStream errorPipedInputStream;
private PipedInputStream outPipedInputStream;
private Thread customOutputThread;
private Thread outOutputThread;
private Thread errorOutputThread;
}

但是结果仅是:启动控制台",没有controller.appendText("Thread" + type + "started");的结果,因此看来该线程无法启动.但是为什么呢?

But the result is only: "Start Console", there are no results of controller.appendText("Thread" + type + "started"); so it seems that this threads don't start. But why?

推荐答案

我认为您必须先加载表单,然后才能获取控制器实例

I think you have to load the form before you can get the controller instance

FXMLLoader loader = new FXMLLoader(this.getClass().getResource("Console.fxml"),resourceBundle);
Parent p = (Parent) loader.load()
controller = loader.getController();
Scene scene = new Scene(p);

这篇关于如何制作自定义Java/JavaFX控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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