如何使用javafx和fxml中的ImageView组件显示图像?

我想这是一件非常简单的事情,但我无法支持它。 我想要的只是在链接到fxml的ImageView上显示图像。 这是我的代码:

package application; import java.io.File; import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.AnchorPane; import javafx.fxml.FXML; import javafx.fxml.FXMLLoader; public class Main extends Application { @FXML private ImageView imageView; @Override public void start(Stage primaryStage) { try { AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("Sample.fxml")); Scene scene = new Scene(root,400,400); scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm()); primaryStage.setTitle("Hello World"); File file = new File("src/Box13.jpg"); Image image = new Image(file.toURI().toString()); imageView = new ImageView(image); //root.getChildren().add(imageView); primaryStage.setScene(scene); primaryStage.show(); } catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { launch(args); } } 

还有我的fxml文件

            

文件链接应该没有问题,因为当我包含outcommented行时它工作正常。 这将是它在java中的方式,但我想在这里使用fxml,因为我使用fxml用于所有其他组件,但它只是不适用于ImageView,我不知道为什么。 我还尝试创建一个新的控制器类并将ImageView链接到那里,但这两者都不起作用。 谁能帮我?

谢谢

如果你想使用FXML,你应该将控制器分开(就像你在使用SampleController一样)。 然后你的FXML中的fx:controller应该指向那个。

您可能缺少控制器中的initialize方法,该方法是Initializable接口的一部分。 在加载FXML后调用此方法,因此我建议您在此处设置图像。

您的SampleController类必须是这样的:

 public class SampleController implements Initializable { @FXML private ImageView imageView; @Override public void initialize(URL location, ResourceBundle resources) { File file = new File("src/Box13.jpg"); Image image = new Image(file.toURI().toString()); imageView.setImage(image); } } 

我在这里测试了它的工作原理。

您不需要初始化程序,除非您每次都动态加载不同的图像。 我认为在fxml中尽可能多地做更有条理的事情。 这是一个fxml文件,可以满足您的需求。

              

在Image标记中指定backgroundLoading属性是可选的,它默认为false。 当加载图像花费片刻或更长时间时,最好将backgroundLoading设置为true,这样就可以使用占位符直到图像加载,并且程序在加载时不会冻结。

 @FXML ImageView image; @Override public void initialize(URL url, ResourceBundle rb) { image.setImage(new Image ("/about.jpg")); } 

请在下面找到使用JavaFX加载图像的示例。

  import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.StackPane; import javafx.stage.Stage; public class LoadImage extends Application { public static void main(String[] args) { Application.launch(args); } @Override public void start(Stage primaryStage) { primaryStage.setTitle("Load Image"); StackPane sp = new StackPane(); Image img = new Image("javafx.jpg"); ImageView imgView = new ImageView(img); sp.getChildren().add(imgView); //Adding HBox to the scene Scene scene = new Scene(sp); primaryStage.setScene(scene); primaryStage.show(); } } 

在项目中创建一个名为Image的源文件夹,并将图像添加到该文件夹​​,否则可以直接从外部URL加载图像,如下所示。

Image img = new Image(“ http://sofzh.miximages.com/java/javafx_logo_color_1.jpg ”);

建议将图像放到资源上,而不是像这样使用它:

 imageView = new ImageView("/gui.img/img.jpg"); 

SRC /样品/图像/ shopp.png

 ** Parent root =new StackPane(); ImageView ımageView=new ImageView(new Image(getClass().getResourceAsStream("images/shopp.png"))); ((StackPane) root).getChildren().add(ımageView); **