3

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

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()'调用的:

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

在异常期间, 中没有打印任何内容textAreaConsole.fxml但会引发以下异常:

线程“JavaFX 应用程序线程”中的异常异常:从线程“JavaFX 应用程序线程”中的 UncaughtExceptionHandler 引发的 java.lang.NullPointerException

我究竟做错了什么?

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

在了解有必要使用多个线程后,我有以下代码:

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");所以似乎这个线程没有启动。但为什么?

4

1 回答 1

2

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

FXMLLoader loader = new FXMLLoader(this.getClass().getResource("Console.fxml"),resourceBundle);
Parent p = (Parent) loader.load()
controller = loader.getController();
Scene scene = new Scene(p);
于 2013-07-31T23:42:08.857 回答