簡體   English   中英

JAVAFX:找不到文件的相對路徑

[英]JAVAFX: cannot find the relative path to a file

我需要將文件style.css作為樣式表添加到場景中。 我正在試圖在包JustAnotherChessGame.model包中的ScreenController類中執行此操作。 該文件位於同一目錄中,但位於不同的程序包中。 如何找到相對路徑? 項目結構

使用Class.getResource訪問應用程序中捆綁的文件:

scene.getStylesheets().add(MainView.class.getResource("style.css").toString());

您將需要導入MainView,以便編譯器識別其名稱。

如果在ScreenController場景是sc而窗格是p

Scene sc = new Scene(p, 400, 300);
sc.getStylesheets().add("/justAnotherChessGame/view/style.css");
primaryStage.setScene(sc);

您需要研究這種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM