4

我有这个在 JavaFX 8 应用程序中加载图像的 css 文件:

#pill-left {  
    -fx-padding: 5;  
     -fx-border-image-source: url("/com/dx57dc/images/left-btn.png");  
    -fx-border-image-slice: 4 4 4 4 fill;  
    -fx-border-image-width: 4 4 4 4;  
    -fx-border-image-insets: 0;  
    -fx-border-image-repeat: stretch;  
     -fx-background-color: null !important;  
}  
#pill-left:selected { -fx-border-image-source: url("/com/dx57dc/images/left-btn-selected.png"); }  
#pill-left .label {  
    -fx-text-fill: #d3d3d3;  
    -fx-effect: dropshadow( one-pass-box , rgba(0,0,0,0.75) , 0, 0.0 , 0 , -1 );  
 }  
#pill-left:selected .label {  
    /* -fx-text-fill: black; */  
    -fx-text-fill: white;  
    -fx-effect: dropshadow( one-pass-box , white , 0, 0.0 , 0 , 1 );  
}  
#pill-center {  
    -fx-padding: 5;  
     -fx-border-image-source: url("/com/dx57dc/images/center-btn.png");  
    -fx-border-image-slice: 4 4 4 4 fill;  
    -fx-border-image-width: 4 4 4 4;  
    -fx-border-image-insets: 0;  
    -fx-border-image-repeat: stretch;  
     -fx-background-color: null !important;  
}  
#pill-center:selected { -fx-border-image-source: url("/com/dx57dc/images/center-btn-selected.png"); }  
#pill-center .label {  
    -fx-text-fill: #d3d3d3;  
     -fx-effect: dropshadow( one-pass-box , rgba(0,0,0,0.75) , 0, 0.0 , 0 , -1 );  
}  
#pill-center:selected .label {  
    -fx-text-fill: black;  
    -fx-effect: dropshadow( one-pass-box , white , 0, 0.0 , 0 , 1 );  
 }  
#pill-right {  
    -fx-padding: 5;  
    -fx-border-image-source: url("/com/dx57dc/images/right-btn.png");  
    -fx-border-image-slice: 4 4 4 4 fill;  
    -fx-border-image-width: 4 4 4 4;  
    -fx-border-image-insets: 0;  
     -fx-border-image-repeat: stretch;  
    -fx-background-color: null !important;  
}  
#pill-right:selected { -fx-border-image-source: url("/com/dx57dc/images/right-btn-selected.png"); }  
#pill-right .label {  
     -fx-text-fill: #d3d3d3;  
    -fx-effect: dropshadow( one-pass-box , rgba(0,0,0,0.75) , 0, 0.0 , 0 , -1 );  
}  
#pill-right:selected .label {  
    -fx-text-fill: black;  
    -fx-effect: dropshadow( one-pass-box , white , 0, 0.0 , 0 , 1 );  
 }  

图像位于 Java 包 com.dx57dc.images

在 Java 7_25 中,此代码按预期工作,但在 JavaFX 8 b99 中出现此错误:

ava.lang.NullPointerException

at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:1129)

at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:1598)

at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1520)

at com.sun.javafx.sg.prism.NGGroup.renderChildren(NGGroup.java:233)

at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:199)

at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:1249)

at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:1598)

at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1520)

at com.sun.javafx.sg.prism.NGGroup.renderChildren(NGGroup.java:233)

at com.sun.javafx.sg.prism.NGGroup.renderContent(NGGroup.java:199)

at com.sun.javafx.sg.prism.NGRegion.renderContent(NGRegion.java:1249)

at com.sun.javafx.sg.prism.NGNode.doRender(NGNode.java:1598)

at com.sun.javafx.sg.prism.NGNode.render(NGNode.java:1520)

at com.sun.javafx.tk.quantum.ViewPainter.doPaint(ViewPainter.java:99)

at com.sun.javafx.tk.quantum.AbstractPainter.paintImpl(AbstractPainter.java:210)

at com.sun.javafx.tk.quantum.PresentingPainter.run(PresentingPainter.java:95)

at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)

at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)

at com.sun.javafx.tk.RenderJob.run(RenderJob.java:58)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

at com.sun.javafx.tk.quantum.QuantumRenderer$PipelineRunnable.run(QuantumRenderer.java:129)

at java.lang.Thread.run(Thread.java:724)

D3D Vram Pool: 13,331,480 used (5.0%), 13,331,480 managed (5.0%), 268,435,456 total

20 total resources being managed

4 permanent resources (20.0%)

1 resources locked (5.0%)

7 resources contain interesting data (35.0%)

0 resources disappeared (0.0%)



D3D Vram Pool: 13,331,480 used (5.0%), 13,331,480 managed (5.0%), 268,435,456 total

20 total resources being managed

4 permanent resources (20.0%)

1 resources locked (5.0%)

7 resources contain interesting data (35.0%)

0 resources disappeared (0.0%)



D3D Vram Pool: 13,331,480 used (5.0%), 13,331,480 managed (5.0%), 268,435,456 total

20 total resources being managed

4 permanent resources (20.0%)

1 resources locked (5.0%)

7 resources contain interesting data (35.0%)

0 resources disappeared (0.0%)

在 Java 8 中从 css 加载图像的正确方法是什么?

4

1 回答 1

11

我有这样的结构:

view
|--css
|   |- style.css
|
|--img
    |- image.png

并像这样使用图像:

 -fx-background-image: url("../img/image.png");

因此,如果

/com/dx57dc/images/

已经相对于您的 css 文件尝试前面的点 ["./com/dx57dc/images/"],否则使用(多个)“../”返回到它们的共享父文件夹

克劳斯

于 2013-07-24T06:12:40.977 回答