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

2024-01-08

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

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

在异常期间,没有任何内容打印在textArea of the Console.fxml,但会引发以下异常:

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

我究竟做错了什么?

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

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

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


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

FXMLLoader loader = new FXMLLoader(this.getClass().getResource("Console.fxml"),resourceBundle);
Parent p = (Parent) loader.load()
controller = loader.getController();
Scene scene = new Scene(p);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何制作自定义 Java/JavaFX 控制台? 的相关文章

随机推荐

  • private(set) 与 let 属性 - 'private(set)' 修饰符不能应用于只读属性

    我已经知道如何private set 作品 但下面的代码给出了编译时错误 class Person private set let name String Error private set let age Int Error init n
  • android 调试器不会在断点处停止

    我在控制台中看到调试语句 但调试器不会在任何断点处停止 我尝试清除所有断点并将它们添加回来 不确定这是如何发生的 但确实如此 对我有用的解决方案 Simply 卸载应用程序从设备 在设备上手动 并再次尝试调试
  • android 的 coredata 相当于什么

    我曾经为 iPhone 编程 但后来我想在 Android 中制作另一个类似的程序 那么 我应该使用类似 coredata 的框架吗 安卓有类似的东西吗 实际上你需要的是检查 Android 的 ORM 工具的持久性 并决定哪一个最适合你
  • 如何从安装程序获取路径以及如何在我的应用程序中设置?

    我正在编写一个 win 应用程序 现在我想为我的应用程序进行设置 我的代码是 Microsoft Win32 RegistryKey rk Microsoft Win32 Registry LocalMachine OpenSubKey S
  • 从 SwiftUI 按钮调用评估Javascript

    假设您有以下 WKWebView 实现 import Combine import SwiftUI import WebKit class WebViewData ObservableObject Published var parsedT
  • 如何在 Python 中对多元对数正态分布进行采样?

    使用 Python 如何从多元对数正态分布中采样数据 例如 对于多元正态分布 有两个选项 假设我们有一个 3 x 3 协方差矩阵和一个 3 维均值向量 mu Method 1 sample np random multivariate no
  • Internet Explorer 中的鼠标移动问题

    当事件目标位于鼠标上方时 Internet Explorer 不会触发 onmousemove 事件 img 元素 缺乏背景 但当目标有背景时 它确实会注册事件 有人对此有解释吗 我在 IE10 IE9 和 IE8 中也有同样的行为 在这里
  • sed,用该行的第一部分替换全局分隔符

    可以说我有以下几行 1 a b c 2 d e f 3 a b 4 a b c d e f 我如何使用 sed 或 perl 编辑它以便阅读 1a1b1c 2d2e2f 3a3b 4a4b4c4d4e4f 我已经用 awk 完成了这样的操作
  • d3 访问分组条形图中的嵌套数据

    我正在通过嵌套 csv 文件构建分组条形图 该图表也可以作为折线图查看 因此我想要一个适合折线对象的嵌套结构 我原来的 csv 看起来像这样 Month Actual Forecast Budget Jul 14 200000 74073
  • XCode - 手动升级到 Swift 4

    我有一个有 2 个目标的项目 两个目标共享超过 99 的代码 并且都是用 Swift 3 编写的 当 XCode 9 可用时 正在扫描其中一个目标 代码和目标的 构建设置 都针对 Swift 4 进行了更新 遗憾的是 我刚刚发现另一个目标仍
  • MATLAB:枚举任意数量的集合中项目的所有组合[重复]

    这个问题在这里已经有答案了 可能的重复 Matlab 生成某些向量元素的所有可能组合 https stackoverflow com questions 4165859 matlab generate all possible combin
  • 如何使用 Hive 查询结构体数组(get_json_object)?

    我将以下 JSON 对象存储在 Hive 表中 main id qwert features scope scope1 name foo value ab12345 age 50 somelist abcde fghij scope sco
  • 是什么导致我的 SKAction 计时器表现异常?

    好吧 我有一个场景 我有这个方法 创建场景内容 当是否移动到视图被叫 在这个方法中 我有一些创建场景的东西 包括一个生成节点的计时器 如下所示 self spawningSpeed 1 5 self enemyData Enemy allo
  • 如何为单个包生成覆盖率 xml 报告?

    我在用着nose and coverage生成覆盖率报告 我现在只有一包ae 所以我指定只涵盖 nosetests w tests unit with xunit with coverage cover package ae 这是结果 看起
  • HTML5 中是否不需要 ALT 属性值来通过验证?

    我正在看这张幻灯片http www slideshare net CharJTF structurals semantics controls and more html 5 is here 3523971 http www slidesh
  • Java 类路径找不到 MySQL 驱动程序

    每当我运行以下代码时 import com mysql jdbc Driver public void insertIntoMysql Print out classloader information ClassLoader cl Cla
  • 如何应用KineticJS过滤器?

    我发现 KineticJS 滤镜文档非常令人沮丧 并且无法在网上找到示例 尤其是考虑到亮度 Kinetic filters 文档link http kineticjs com docs symbols Kinetic Filters php
  • 单击球体

    我有一个以正交投影为中心绘制的单位球体 半径为 1 球体可以自由旋转 如何确定用户单击球体上的点 Given 显示器的高度和宽度 投影圆的半径 以像素为单位 用户点击点的坐标 假设左上角为 0 0 则 x 值随着向右移动而增加 y 值随着向
  • 为什么 Grpc.Core NuGet 包这么大?

    最新的 Grpc Core NuGet 包有 150 MB 之大 它似乎在其一生中稳步增长 这里有一些例子 v1 17 1 2018 年 12 月 48 34 MB v2 23 1 2019 年 8 月 68 11 MB v2 30 0 2
  • 如何制作自定义 Java/JavaFX 控制台?

    有必要制作一个自定义控制台 我有以下代码 public class Console extends OutputStream private Console ResourceBundle resourceBundle throws IOEx