簡體   English   中英

JavaFX-在應用程序啟動時打開多個窗口

[英]JavaFX - opening multiple windows on application startup

關於如何在按下按鈕時打開新窗口有幾個問題,但是我想在啟動應用程序時打開兩個窗口。

我當前的方法是將以下代碼放在一個新類中,該新類充當新窗口的控制器:

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("secondWindow.fxml"));
        fxmlLoader.setController(this);
        try {
            parent = (Parent) fxmlLoader.load();
            scene = new Scene(parent, 500, 400);
            stage = new Stage(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }

這對於按鈕或基於事件的窗口打開非常有用, 我正在尋找同時啟動兩個窗口的功能 因此,我想使用main方法從類中啟動第二個窗口。

在此類中,您可以找到使用以下代碼啟動的第一個窗口:

Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();

在下面,我想添加代碼以啟動第二個窗口。 我試過了:

Parent secondRoot = FXMLLoader.load(getClass().getResource("secondWindow.fxml"));
        Scene secondScene = new Scene(secondRoot);
        Stage secondStage = new Stage();
        secondStage.setScene(secondScene);
        secondStage.show();

據我所知應該這樣做,但是會出現以下錯誤:

java.lang.NoSuchMethodException: monopolybank.SecondWindowController.<init>()
    at java.lang.Class.getConstructor0(Class.java:2971)
    at java.lang.Class.newInstance(Class.java:403) 

如何解決我的方法或獲得相同結果的替代方法是什么?

您的問題與窗口數無關,而與構造函數有關,該構造函數的參數已添加到monopolybank.SecondWindowController創建的類=>從該類中刪除構造函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM