简体   繁体   English

JavaFX - 从另一个类事件打开一个场景

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

I've got a Main.java which holds all of my JavaFX code, multiple scenes of an entire program are stored here and it's become very difficult to read.我有一个Main.java ,它保存了我所有的 JavaFX 代码,整个程序的多个场景都存储在这里,阅读起来变得非常困难。

I'd like to try to store each "screen"( Scene ) in a separate .java class so that I may more easily find and make changes etc. I'm new to JavaFX and still learning OOP.我想尝试将每个“屏幕”( Scene )存储在单独的 .java 类中,以便我可以更轻松地找到和进行更改等。我是 JavaFX 的新手,仍在学习 OOP。

In the sample below I would like to open a new Scene (from a separate .java class) on a Button click event.在下面的示例中,我想在Button单击事件上打开一个新Scene (来自单独的 .java 类)。 I have this currently working in the single class.我目前在单班工作。 But don't know how to approach this in another file.但不知道如何在另一个文件中解决这个问题。 I also need to pass two array lists into the separate class to populate tables.我还需要将两个数组列表传递到单独的类中以填充表。

Below is a sample of my tinkering.下面是我的修补示例。

The Button : 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));

The new class: (Just a screen with more buttons and soon two tables to display the passed array lists)新类:(只是一个带有更多按钮的屏幕,很快就会有两个表来显示传递的数组列表)

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;
    }
}

Thanks in advance.提前致谢。

You can extend Scene with all of your classes, doing something like public class SceneA extends Scene and create a constructor like您可以使用所有类扩展Scene ,执行类似public class SceneA extends Scene并创建一个构造函数,例如

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

Then:然后:

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