0
我有一個assets
文件夾,裏面裝滿了要在我創建的遊戲中實現的圖像,我希望在窗口中顯示這些圖像。這是我的代碼到目前爲止。JavaFX ImageView圖像不顯示
public class javafxtest extends Application {
public void start(Stage primaryStage) throws Exception {
Group root = new Group();
Scene scene = new Scene(root, 600, 600);
Image test = new Image("file:assets/BA.png");
ImageView piece = new ImageView(test);
piece.setX(10);
piece.setY(10);
Rectangle rct = new Rectangle(50, 150, 500, 300);
rct.setFill(Color.GRAY);
root.getChildren().addAll(rct, piece);
primaryStage.setScene(scene);
primaryStage.show();
}
}
這將是一個程序運行IntelliJ /從一個罐子。資產文件夾與此文件位於同一目錄中。我沒有得到任何文件路徑錯誤,所以我認爲它可以找到圖像,但它並沒有出現在屏幕上,矩形也沒有。公平的警告,我從頭開始學習JavaFX,我發現沒有太多解釋事情是如何工作的,所以這可能是一個愚蠢的問題。
如果圖像被入瓶中,你應該通過'getResource'訪問:'新的圖像(的getClass()的getResource( 「/資產/ BA.png」)toExternalForm()。 )' – fabian
只需要在intellij中,所以上面的結果爲空指針異常對我來說 – 420fedoras
您需要將您的圖像添加到您的項目並使用'getRessource()',或者您需要編寫圖像的完整路徑。 –