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

Java 像BorderPane这样的类在以后编码而不是初始化时如何显示更改?,java,javafx,javafx-8,default-constructor,borderpane,Java,Javafx,Javafx 8,Default Constructor,Borderpane,我当时正在研究JavaFX,我意识到了一些让我思考的东西。因此,在我的构造函数中有一个场景,它添加了类型BorderPane的变量root。在主类中,首先调用该类的构造函数,从而初始化场景中的BorderPane。但是,BorderPane正在一个名为build的方法中修改和编辑。我对BorderPane如何仍然能够从build()显示其更新版本感到困惑,因为在构造函数中它已经被命令显示 这是我的密码: public class SnacksOrDrinks { pri

我当时正在研究JavaFX,我意识到了一些让我思考的东西。因此,在我的构造函数中有一个
场景
,它添加了类型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);
        }
    }
公共类零食或饮料
{
私有边界根;
私人阶段初级阶段;
私密场景;
私家车阴极射线管;
公共零食或饮料(舞台初级舞台、购物车crt)
{
//正在初始化BorderPane并将其放置在场景中:
BorderPane root1=新的BorderPane();
this.scene=新场景(root1800475);
this.crt=crt;
ImageView imgview=新的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背景色:蓝色”);
root.getChildren().add(imgview);
}
公共无效构建(分发器dis)
{
//ItemsPaneProp扩展GridPane并存储在其他类中共享的公共属性。
ItemsPaneProp窗格=新建ItemsPaneProp();
//创建的标题:
CommHeaderProp header=新CommHeaderProp(“Lopes自动售货机”);
//然而,BorderPane在这里被修改,构造函数已经被调用。
边框窗格。设置对齐(标题、位置中心);
root.setTop(头文件);
//创建一行:
直线=新线(100、10、300、10);
线。刚毛(颜色。黄花);
线。设置行程宽度(2);
line.setSmooth(真);
添加(行,0,0);
//创建所有场景的组件并设置事件处理程序
ImageView img=新的ImageView(“./application/softdrink.jpg”);
img.setFitWidth(75);
安装高度(75);
按钮btn1=新建按钮(“选择获取饮料”,img);
btn1.setStyle(“-fx背景色:白色”);
btn1.setFont(Font.Font(“Times New Roman”,FontWeight.BOLD,FontPost姿.REGULAR,12));
btn1.setContentDisplay(ContentDisplay.TOP);
添加(btn1,0,1);
ImageView img1=新的ImageView(“./application/potato chips.png”);
img1.设置宽度(75);
img1.安装高度(75);
按钮btn2=新按钮(“选择以获取零食”,img1);
btn2.setStyle(“-fx背景色:白色”);
btn2.setFont(Font.Font(“Times New Roman”,FontWeight.BOLD,FontPost姿.REGULAR,12));
btn2.setContentDisplay(ContentDisplay.TOP);`在这里输入代码`
窗格。添加(btn2,1,1);
root.setCenter(pane);//这里也在修改BorderPane,但是构造函数没有更新场景。那么,它是如何工作的呢?
btn1.setOnMouseClicked(新的EventHandler()
{
@凌驾
公共无效句柄(MouseEvent e)
{
//创建、构建和显示场景2
DrinksList场景=新的DrinksList(primaryStage,crt);
场景构建(dis);
scene.show();
}
});
setOnMouseClicked(新的EventHandler()
{
@凌驾
公共无效句柄(MouseEvent e)
{
//创建、构建和显示场景2
SnacksList场景=新SnacksList(初级阶段,crt);
场景构建(dis);
scene.show();
}
});
}
公开展览(
{//设置场景:
初级阶段。场景(场景);
}
}
看一看

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


您的borderPane是SceneGraph的一部分,所以JavaFX知道,当您更改borderPane中的任何内容并自动为其调用重画方法时。

您能解释一下您认为应该发生什么吗?当GUI状态被修改时,JavaFX会自动重新绘制相关图形(它以优化的方式进行)。