gpt4 book ai didi

java - 如何制作自定义 Java/JavaFX 控制台?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:28:33 27 4
gpt4 key购买 nike

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

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;
}

Controller 文件:

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)));
}

在异常期间,Console.fxmltextArea 中没有打印任何内容,但抛出了以下异常:

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

我做错了什么?

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

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

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;
}

但结果只是:"Start Console",没有controller.appendText("Thread"+ type + "started");的结果,所以看起来这个线程没有开始。但是为什么?

最佳答案

我认为你必须先加载表单才能获取 Controller 实例

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

关于java - 如何制作自定义 Java/JavaFX 控制台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17981938/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com