2

我正在研究 JavaFX,我意识到一些让我思考的事情。因此,scene在我的构造函数中有一个添加了 BorderPane 类型的变量 root。在主类中,首先调用这个类的构造函数,从而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);
        }
    }
4

1 回答 1

0

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

JavaFX 场景图是一个保留模式 API,这意味着它维护应用程序中所有图形对象的内部模型。在任何给定时间,它都知道要显示哪些对象,屏幕的哪些区域需要重新绘制,以及如何以最有效的方式渲染它们。

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

于 2018-12-15T09:43:11.297 回答