简体   繁体   English

从FXML转换为JAVA时不加载图像

[英]Image doesn't load when converting from FXML to JAVA

I have converted a FXML file to Java and everything works except one image 我已经将FXML文件转换为Java,并且除一张图片外,其他所有东西都可以使用

import java.io.File;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.shape.StrokeType;
import javafx.scene.text.Text;
import javafx.stage.Stage;
 public class Screen extends Application {
@Override
    public void start(Stage primaryStage){
        primaryStage.setTitle("Player Screen");
        GridPane grid = new GridPane();
        Text coins = new Text();
        coins.setStrokeWidth(0.0);
        coins.setStrokeType(StrokeType.OUTSIDE);
        GridPane.setColumnIndex(coins, 1);
        coins.setId("coins");
        coins.setText("11");
        coins.setWrappingWidth(41.46875);

This is the code part of the image: 这是图像的代码部分:

        ImageView imageTile = new ImageView();
        imageTile.setPickOnBounds(true);
        imageTile.setFitWidth(200.0);
        GridPane.setColumnIndex(imageTile, 17);
        imageTile.setFitHeight(150.0);
        GridPane.setRowIndex(imageTile, 2);
        imageTile.setPreserveRatio(true);
        File file = new File("/excommunicationTile1.jpg");
        Image image = new Image(file.toURI().toString());
        imageTile.setImage(image);

Both the image and the css file are in the same folder so i don't understand why it loads the css information but not the image. 图像和css文件都在同一个文件夹中,所以我不明白为什么它加载css信息而不是图像。

        grid.getChildren().add(coins);
        grid.getChildren().add(imageTile);
        Scene scene = new Scene(grid,546,200);
        primaryStage.setScene(scene);
        scene.getStylesheets().add(getClass().getResource("/style.css").toExternalForm());
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

Replace File file = new File("/excommunicationTile1.jpg"); 替换File file = new File("/excommunicationTile1.jpg"); to

InputStream inputStream = this.getClass().getResourceAsStream("/excommunicationTile1.jpg")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM