簡體   English   中英

JavaFX - 從另一個類事件打開一個場景

[英]JavaFX - Open a Scene from another class event

我有一個Main.java ,它保存了我所有的 JavaFX 代碼,整個程序的多個場景都存儲在這里,閱讀起來變得非常困難。

我想嘗試將每個“屏幕”( Scene )存儲在單獨的 .java 類中,以便我可以更輕松地找到和進行更改等。我是 JavaFX 的新手,仍在學習 OOP。

在下面的示例中,我想在Button單擊事件上打開一個新Scene (來自單獨的 .java 類)。 我目前在單班工作。 但不知道如何在另一個文件中解決這個問題。 我還需要將兩個數組列表傳遞到單獨的類中以填充表。

下面是我的修補示例。

Button

goToNextScene = new Button("View next scene");
goToNextScene.setOnAction(e -> window.setScene(AnotherClassA.sceneLayout, aList, bList));
// another try:
goToNextScene.setOnAction(e -> AnotherClassA.sceneLayout(aList, bList));

新類:(只是一個帶有更多按鈕的屏幕,很快就會有兩個表來顯示傳遞的數組列表)

public class AnotherClassA {

    private BorderPane bP;
    private HBox hBtnMenu;
    private Button hMenuBtnA, hMenuBtnB, returnBtn;
    public Scene sceneLayout;

    public Scene sceneLayout(ArrayList aList, ArrayList bList) {

       // array lists not used yet, but will come

        hBtnMenu = new HBox();
        hBtnMenu.setSpacing(10);
        hBtnMenu.setPadding(new Insets(10,10,10,10));

       hMenuBtnA = new Button("Btn A");
       //hMenuBtnA.setOnAction(e -> window.setScene(AnotherClassA.sceneA));

       hMenuBtnB = new Button("Btn B");
       //hMenuBtnB.setOnAction(e -> window.setScene(AnotherClassB.sceneB));

       returnBtn = new Button("Return to Home");
       //returnBtn.setOnAction(e -> window.setScene(Main.splashScene));

       hBtnMenu.getChildren().addAll(hMenuBtnA, hMenuBtnB, returnBtn);

       bP = new BorderPane();
       bP.setTop(hBtnMenu);

       animalSceneLayout = new Scene(bP, 1200, 760);

       return asceneLayout;
    }
}

提前致謝。

您可以使用所有類擴展Scene ,執行類似public class SceneA extends Scene並創建一個構造函數,例如

public SceneA(Parent root, ArrayList a, ArrayList b) { super(root); } 

然后:

goToNextScene.setOnAction(e -> window.setScene(new SceneA(new AnchorPane(), aList, bList));

暫無
暫無

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

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