如何在自己的线程中启动JavaFXGUI
我有一个项目,在那里我需要启动一个GUI,但在另一个不是我的主线程的线程中 ->第一个主线程开始 ->如果它决定显示GUI,它将启动我的GUI ->所有其他计算仍应在主线程中进行 我的意思是,在任何情况下,我都不能在主线程中启动gui。我需要能够与我的控制器通信(示例中没有控制器)。但是当我做正常的事情时:。。开始扩展应用程序{..方法,我无法再与控制器通信,因为线程已被占用。下面的代码应该允许我执行所有需要执行的操作,因此我希望有一种方法可以使其正常工作 问题是,为什么我的代码不起作用是一个例外: 线程“thread-0”java.lang.IllegalStateException中出现异常:工具箱未初始化 在我的GuiThread类中调用Platform.runLater()时 提前谢谢你的帮助 我的主要班级:如何在自己的线程中启动JavaFXGUI,java,multithreading,user-interface,javafx,Java,Multithreading,User Interface,Javafx,我有一个项目,在那里我需要启动一个GUI,但在另一个不是我的主线程的线程中 ->第一个主线程开始 ->如果它决定显示GUI,它将启动我的GUI ->所有其他计算仍应在主线程中进行 我的意思是,在任何情况下,我都不能在主线程中启动gui。我需要能够与我的控制器通信(示例中没有控制器)。但是当我做正常的事情时:。。开始扩展应用程序{..方法,我无法再与控制器通信,因为线程已被占用。下面的代码应该允许我执行所有需要执行的操作,因此我希望有一种方法可以使其正常工作 问题是,为什么我的代码不起作用是一个例
public class Start{
private void start() {
GuiThread thread = new GuiThread();
thread.start();
System.out.println("here continues the thread, while the GUI is shown");
}
public static void main(String[] args) {
Start main = new Start();
main.start();
}
}
我的自定义线程类,GUI应在其中启动:
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class GuiThread extends Thread {
@Override
public void run() {
super.run();
// Here i have objects i need to have in the controller
// Then i have to start my GUI
// When calling Platform.runLater()... this error shows : Exception in thread "Thread-0" java.lang.IllegalStateException: Toolkit not initialized
Platform.runLater(() -> {
Group root;
try {
Stage stage = new Stage();
root = new Group();
Scene scene = new Scene(root);
FXMLLoader loader = new FXMLLoader(getClass().getResource("myGui.fxml"));
Node node = loader.load();
// Controller stuff
root.getChildren().add(node);
stage.setScene(scene);
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
});
}
}
只是一个示例FXML文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.StackPane?>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
<children>
<StackPane prefHeight="150.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
<children>
<Label text="left" />
</children>
</StackPane>
<StackPane prefHeight="150.0" prefWidth="200.0" HBox.hgrow="ALWAYS">
<children>
<Label text="right" />
</children>
</StackPane>
</children>
</HBox>
通常,您会从
应用程序.init
或应用程序.start
方法启动任何其他线程,但在您的情况下,这似乎不是一个选项
从JavaFX9开始,您可以在第一次需要访问JavaFX时使用。在执行传递给该方法的Runnable
后,您应该能够使用Platfrom.runLater
,就像您习惯的那样
使用这种方法,您需要确保在所有其他逻辑完成时关闭,并且您不需要显示任何GUI
Platform.startup(() -> {
Group root;
try {
Stage stage = new Stage();
root = new Group();
Scene scene = new Scene(root);
FXMLLoader loader = new FXMLLoader(getClass().getResource("myGui.fxml"));
Node node = loader.load();
// Controller stuff
root.getChildren().add(node);
stage.setScene(scene);
stage.setOnHidden(evt -> Platform.exit()); // make sure to completely shut down JavaFX when closing the window
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
});
如果
启动
扩展
应用程序
,会发生什么情况?请参阅相关问题和答案。