您好我正在尝试实现一个在LAN上传输文件/文本的简单套接字。当然,我使用JavaFX应用程序线程来显示主UI(客户端线程),并使用一个单独的线程来实现ServerSocket
"接受"传入连接(服务器线程)。在加载UI时,当我尝试在服务器线程中显示Alert
时,它不会让我,因为我不再使用JavaFX应用程序线程了。我试着:
在服务器线程中使用Platform.runlater()
来调用Alert
,如下所示:
//inside the run() method of the Server class, which implements Runnable
Platform.runLater(() -> {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setContentText("This shows that it works.");
alert.show();
});
//and in Main class, which defines and displays GUI
Thread server = new Thread(new Server());
server.start();
JFXPanel
,它应该隐式实例化一个JavaFX Application对象(我猜)javafx.concurrent.Task
并创建一个新的javafx.concurrent.Service
子类,用于实例化服务器" Task
" 但到目前为止,我无法让它发挥作用。对于第一种和第三种方法,GUI无限期挂起,对于第二种方法,抛出IllegalStateException
表示我不在JavaFX应用程序线程上。
有没有办法在JavaFX Application线程以外的线程中安全地显示Alert
(或其他组件,如FileChooser
)?谢谢!