【发布时间】:2023-04-11 04:51:01
【问题描述】:
我有一个 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;
}
}
提前致谢。
【问题讨论】:
-
您可以使用
public class SceneA extends Scene这样的所有类扩展 Scene 并创建像public SceneA(Parent root, ArrayList a, ArrayList b) { super(root); }这样的构造函数然后:goToNextScene.setOnAction(e -> window.setScene(new SceneA(new AnchorPane(), aList, bList)); -
@DVarga 感谢您的回复,这正是我要找的! :) 完美运行!如果您将此作为答案发布,我可以向您发送一些互联网点:) 再次感谢