繁体   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