In JavaFX, I’m haunted by the problem of loading a picture onto a form. No matter how I try to load it, the result is a RuntimeException. What am I doing wrong ?

public class Main extends Application { @Override public void start(Stage primaryStage) throws Exception { Pane root = new Pane(); Label label = new Label(); // метка с текстом label.setText("This is a label"); Image image = new Image(getClass().getResourceAsStream("D:\\1.png")); // тут RuntimeException ImageView img = new ImageView(image); img.setFitHeight(200); img.setFitWidth(250); Label labelImg = new Label(); labelImg.setGraphic(img); labelImg.setTranslateX(75); labelImg.setTranslateY(100); root.getChildren().addAll(label, labelImg); Scene scene = new Scene(root, 400, 400); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } 

}

Tried with a text file - loaded from disk without problems. So the path is right pointing. How can this be caused?

    1 answer 1

    Try uploading an image like this:

     File file = new File("image.png"); Image image = new Image(file.toURI().toString()); ImageView iv = new ImageView(image); 
    • There are no exceptions, but the picture is not drawn. - faoxis