像 BorderPane 这样的 class 如何在稍后编码而不是初始化时显示更改?

How can a class like BorderPane display the change when it is later being coded and not when initialised?

我正在研究 JavaFX,我意识到了一些让我不断思考的事情。因此,我的构造函数中有一个 scene,它添加了 BorderPane 类型的变量根。在主class中,首先调用这个class'构造函数,从而初始化场景中的BorderPane。然而,BorderPane 正在一个名为 build 的方法中被修改和编辑。我很困惑 BorderPane 如何仍然能够从 build() 显示其更新版本,因为在构造函数中它已经被命令显示。

这是我的代码:

 public class SnacksOrDrinks
    {
        private BorderPane root;
        private Stage primaryStage;
        private Scene scene;
        private Cart crt;

        public SnacksOrDrinks(Stage primaryStage, Cart crt)
        {
        // BorderPane is being initialised and is being put inside the scene: 
                BorderPane root1 = new BorderPane();        
                this.scene = new Scene(root1, 800, 475);
                this.crt = crt;

            ImageView imgview = new ImageView("./application/MountainPainting.jpg");
            imgview.fitWidthProperty().bind(primaryStage.widthProperty());
            imgview.fitHeightProperty().bind(primaryStage.heightProperty());

            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            this.primaryStage = primaryStage;
            this.root = root1;
            root.setStyle("-fx-background-color: blue");


            root.getChildren().add(imgview);
        }

        public void build(Dispenser dis)
        {
            // ItemsPaneProp extends GridPane and stores the common properties shared in other classes.
            ItemsPaneProp pane = new ItemsPaneProp();       

            // Header Created: 
            CommHeaderProp header = new CommHeaderProp("Lopes Vending Machine");


    // However, BorderPane is being modified here, and the constructor has already been called. 
            BorderPane.setAlignment(header, Pos.CENTER);
            root.setTop(header);

            // Create a Line: 
            Line line = new Line(100, 10, 300, 10);
            line.setFill(Color.GOLDENROD);
            line.setStrokeWidth(2);
            line.setSmooth(true);
            pane.add(line, 0, 0);

            // Create all the Scene's Components and setup the Event Handlers
            ImageView img = new ImageView("./application/softdrink.jpg");
            img.setFitWidth(75);
            img.setFitHeight(75);
            Button btn1 = new Button("Select to get Drinks", img);
            btn1.setStyle("-fx-background-color: white");
            btn1.setFont(Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 12));
            btn1.setContentDisplay(ContentDisplay.TOP);
            pane.add(btn1, 0, 1);

            ImageView img1 = new ImageView("./application/potato-chips.png");
            img1.setFitWidth(75);
            img1.setFitHeight(75);
            Button btn2 = new Button("Select to get Snacks", img1);
            btn2.setStyle("-fx-background-color: white");
            btn2.setFont(Font.font("Times New Roman", FontWeight.BOLD, FontPosture.REGULAR, 12));
            btn2.setContentDisplay(ContentDisplay.TOP);`enter code here`
            pane.add(btn2, 1, 1);

            root.setCenter(pane); // The BorderPane is being modified here too, but the constructor isn't updating the scene. Then, how does it still work?

            btn1.setOnMouseClicked(new EventHandler<MouseEvent>()
            {
                @Override
                public void handle(MouseEvent e)
                {
                    // Create, build, and show Scene 2
                    DrinksList scene = new DrinksList(primaryStage, crt);
                    scene.build(dis);
                    scene.show();
                }
            });

            btn2.setOnMouseClicked(new EventHandler<MouseEvent>()
            {
                @Override
                public void handle(MouseEvent e)
                {
                    // Create, build, and show Scene 2
                    SnacksList scene = new SnacksList(primaryStage, crt);
                    scene.build(dis);
                    scene.show();
                }
            });

        }

        public void show()
        {// setting scene:
            primaryStage.setScene(scene);
        }
    }

看看https://docs.oracle.com/javase/8/javafx/scene-graph-tutorial/scenegraph.htm#JFXSG107

The JavaFX scene graph is a retained mode API, meaning that it maintains an internal model of all graphical objects in your application. At any given time, it knows what objects to display, what areas of the screen need repainting, and how to render it all in the most efficient manner.

您的 borderPane - SceneGraph 的一部分,因此 JavaFX 知道,当您更改 borderPane 中的任何内容时,并自动为其调用重绘方法。