我已将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);
这是图片的代码部分:
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);
图片和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);
}
}
答案 0 :(得分:0)
替换File file = new File("/excommunicationTile1.jpg");
到
InputStream inputStream = this.getClass().getResourceAsStream("/excommunicationTile1.jpg")