JavaFX image loading error

前端 未结 2 552
醉梦人生
醉梦人生 2021-01-27 23:15

I\'m trying to implement a very simple interface to Raspberry Pi in JavaFX. I\'m using an .fxml based layout and styling my items with css. My problem is despite the app works p

相关标签:
2条回答
  • 2021-01-27 23:16

    The problem with your project is in the resources folder. Being outside the source folder is not found.

    This is how I made it work:

    Created a JavaFX project in NetBeans, and moved the resource folder inside the source one. So this is Source Packages:

    -Source Packages
        +me.noip.blase
        +me.noip.blase.view
        +resources.images
    

    and then changed all references from file: to "/":

    primaryStage.getIcons().add(new Image("/resources/images/icon.png"));
    

    and the css file:

    .imageButton1 {
            -fx-background-color: blue;
            -fx-graphic: url('/resources/images/temperature.png');
    }
    .imageButton2 {
            -fx-graphic: url('/resources/images/gear.png');
            -fx-background-color: red;
    }
    .imageButton3 {
            -fx-graphic: url('/resources/images/power.png');
            -fx-background-color: black;
    }
    
    .imageButton4 {
            -fx-graphic: url('/resources/images/diagram.png');
            -fx-background-color: green;
    }
    

    Now it works fine in both desktop and Raspberry Pi.

    0 讨论(0)
  • 2021-01-27 23:43

    I will accept your solution as an answer, I tried importing to NetBeans and it worked, but I cannot reproduce that under Eclipse. Meanwhile I found another question here and followed that solution and it works(I dont know how I didn't find that before, I massively googled for a day)

    I will leave this here if anyone else searchs for this.

    Thank you very much for your time and help :)

    0 讨论(0)
提交回复
热议问题