JavaFX: Add an image to the GridPane

2019-09-14 16:09发布

问题:

I want to put an Image into a GridPane.

The URI ist existant, I imported javafx.scene.image, I declared everything. Why does it not work? It just gives me a blank window. Even with a local picture its not working. And when I for example write something like "file:sdhasidf.png" (Not existant btw.), its just a blank window, too. I'm just getting a UnknownProtocol Exception if I mess up the protocol, so it gets loaded.

Image img = new Image("https://cdn.discordapp.com/attachments/250163910454280192/296377451599364107/Untitled.png");
ImageView imgView = new ImageView(img);

GridPane.setConstraints(imgView, 5, 0);
gp.getChildren().add(imgView);  //gp ist a GridPane

Scene scene = new Scene(gp, 500, 600);
window.setScene(scene);
window.show();

Thanks for reading :)