0
我遇到了JavaFX多線程問題,如果任何人都可以提供幫助。請參閱下面的代碼。謝謝!試圖打開多個JavaFX GUI窗口出現錯誤?
import ...
public class Session extends Application implements Runnable {
String srcIP;
public Session() {
}
@Override
public void run() {
launch();
System.out.println("New thread running with IP: " + srcIP);
}
public static void main(String args[]) {
new Thread(new Session()).start();
new Thread(new Session()).start();
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
StackPane root = new StackPane();
root.setStyle("-fx-background-color: BLACK;");
primaryStage.setScene(new Scene(root, 400, 330));
primaryStage.show();
}
}
這裏是控制檯輸出。它會運行第一個線程就好了。但第二個線程陷入了以下錯誤。
Exception in thread "Thread-1" java.lang.IllegalStateException: Application launch must not be called more than once
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:94)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:75)
at javafx.application.Application.launch(Application.java:209)
at guiFX.Session.run(Session.java:27)
at java.lang.Thread.run(Thread.java:744)
New thread running with IP: null
我明白了,但我想我可以讓它「可運行」,並讓它運行多個實例?爲什麼這不起作用? – user2799603