Ни один из предыдущих ответов не работал для меня. Тем не менее, этот, который я видел некоторое время назад, но не могу найти оригинальную ссылку, работает безупречно.
@Override
public void start(Stage primaryStage) throws Exception {
//create a pane to hold the image views
Pane pane = new HBox(10);
pane.setPadding(new Insets(5,5,5,5));
//create the image to be used!
Image image = new Image("/Content/vortex.jpg");
//set some custom properties and add an image
ImageView imageView = new ImageView(image);
imageView.setFitHeight(100);
imageView.setFitWidth(100);
pane.getChildren().add(imageView);
//add the second image view with this image and no custom properties
pane.getChildren().add(new ImageView(image));
ImageView imageView2 = new ImageView(image);
imageView2.setRotate(45);
pane.getChildren().add(imageView2);
Scene scene = new Scene(pane, 200, 200);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
Ключ в том, что когда вы создаете изображение, НАЧИНАЙТЕ путь с помощью '/' (то есть: "/Content/vortex.jpg"). Обратите внимание, что в этой настройке корневая папка является папкой src в большинстве IDE.