我正在尝试将图像设置为GridPane的背景。我不使用fxml代码,而是使用普通的JavaFX代码。
public Login() {
grid = new GridPane();
grid.setAlignment (Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25,25,25,25));
Image img = new Image("/src/application/Images/L.png");
ImageView imgView = new ImageView(getClass().getResource("/src/application/Images/L.png").toExternalForm());
imgView.setImage(img);
grid.getChildren().addAll(imgView);
scene = new Scene (grid, 300, 150);
该异常归结为该代码段
> Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
如何解决此问题?
答案 0 :(得分:2)
我从图像路径中删除了src以获得:
Image img = new Image("/application/Images/L.png");
我添加了:
grid.setBackground(
new Background(
new BackgroundImage(
img,
BackgroundRepeat.REPEAT,
BackgroundRepeat.REPEAT,
BackgroundPosition.DEFAULT,
BackgroundSize.DEFAULT
)
)
);
它有效。