如何在Java 中多次呼叫launch()
JavaFX 應用程式啟動方法launch() 旨在被呼叫每個申請僅一次。嘗試多次呼叫 launch() 會導致「IllegalStateException」錯誤。
解決方案:將後續視窗建立包裝在Platform.runLater()
而不是呼叫多次launch(),請考慮以下方法:
範例程式碼:
<code class="java">public class Wumpus extends Application { private static final Insets SAFETY_ZONE = new Insets(10); private Label cowerInFear = new Label(); private Stage mainStage; @Override public void start(final Stage stage) { mainStage = stage; mainStage.setAlwaysOnTop(true); Platform.setImplicitExit(false); cowerInFear.setPadding(SAFETY_ZONE); cowerInFear.setOnMouseClicked(event -> Platform.exit()); stage.setScene(new Scene(cowerInFear)); } public static void main(String[] args) { launch(args); // Another window can be created here by wrapping its creation // and display in a Platform.runLater() block. Platform.runLater(() -> { Stage newStage = new Stage(); newStage.setScene(new Scene(new Label("Another Window"))); newStage.show(); }); } }</code>
注意:
以上是如何在不呼叫 launch() 的情況下建立多個 JavaFX 視窗?的詳細內容。更多資訊請關注PHP中文網其他相關文章!