You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaFx引入CSS文件报错:找不到@style.css资源 请求协助

Fixing "Resource @style.css not found" in JavaFX FXML

Hey there! Let's get your JavaFX CSS working—this error usually boils down to a file path mismatch, so let's walk through the most common fixes:

1. Double-check your file placement

The @style.css path in your FXML is relative to the FXML file's location. So first, make sure your CSS file is in the right spot:

  • If your FXML lives in src/main/resources/sample/ (common for Maven/Gradle projects), style.css needs to be in that exact same sample/ folder.
  • If you want your CSS in the root resources folder (src/main/resources/), update the FXML path to @../style.css (to go up one directory) or use an absolute path like /style.css.

Here's a correct project structure example:

src/
├─ main/
│  ├─ java/
│  │  └─ sample/
│  │     ├─ Controller.java
│  │     └─ Main.java
│  └─ resources/
│     └─ sample/
│        ├─ your_fxml_file.fxml
│        └─ style.css

2. Ensure your CSS is in the classpath

Build tools like Maven/Gradle only copy files from src/main/resources to your compiled classpath. If you accidentally put style.css in src/main/java, it won't be available at runtime—move it to the resources folder instead.

3. Try loading CSS via code (if FXML path still fails)

If the FXML approach isn't working, you can load the CSS directly in your controller or main application class:

// In your Controller class
@Override
public void initialize(URL url, ResourceBundle rb) {
    String cssPath = getClass().getResource("style.css").toExternalForm();
    gridPane.getStylesheets().add(cssPath);
}

// Or in your Main application's start() method
Scene scene = new Scene(FXMLLoader.load(getClass().getResource("your_fxml_file.fxml")));
scene.getStylesheets().add(getClass().getResource("style.css").toExternalForm());

4. Watch for spelling/case sensitivity

JavaFX resource paths are case-sensitive! Make sure style.css is named exactly that—no typos like Style.css or style.CSS.

Give these steps a try—9 times out of 10, it's just a path or placement issue. Let me know if you're still stuck!

内容的提问来源于stack exchange,提问作者alt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 14:18:14