简体   繁体   English

使用在场景构建器中创建的fxml文件从java添加图像

[英]Add image from java using fxml file created in scene builder

If I want to add image node to fxml (created in scene builder) file from java, how can I achieve this ? 如果我想将图像节点添加到java中的fxml(在场景构建器中创建)文件中,我该如何实现呢?

private ImageView close;
private ImageView close;
Image i = new Image(“file:C:\\Users\\Tarun\\Desktop\\Close-icon.png”);
close = new ImageView();
close.setImage(i);
close.setFitHeight(20);
close.setFitWidth(20);
close.setX(570);
close.setY(10);
Parent root = FXMLLoader.load(getClass().getResource(“table.fxml”));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();

I know that there is some method root.getchidren.add(close) but here I am not able to implement this method. 我知道有一些方法root.getchidren.add(close)但在这里我无法实现这个方法。

You need to cast Parent to an appropriate class. 您需要将Parent转换为适当的类。 Eg 例如

AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource(“table.fxml”));
root.getChidren.add(close);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM