JavaFX 图像加载错误 [英] JavaFX image loading error

查看:41
本文介绍了JavaFX 图像加载错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 JavaFX 中实现一个非常简单的 Raspberry Pi 接口.我正在使用基于 .fxml 的布局并使用 css 为我的项目设置样式.我的问题是,尽管该应用程序在我的主计算机上运行良好(从 eclipse 运行),但它无法在 Raspberry 上运行,也不能在我尝试在主计算机上运行导出的 jar 时运行.

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 perfectly in my main computer(running from eclipse) it's not working on Raspberry nor when I try to run the exported jar on main computer.

这就是我为按钮设置外观的方式.当然资源/图像文件夹在我的构建路径中.按钮具有我在 css 中描述的颜色,但图像未加载.

This is how I skinned my button. Ofcourse resources/images folder is in my building path. The buttons has the color what I described in css, but the image is not loading.

.imageButton1 {
-fx-background-color: blue;
-fx-graphic: url('file:resources/images/temperature.png');}

它给了我以下错误:

警告:加载图像时出错:file:resources/images/temperature.png

WARNING: Error loading image: file:resources/images/temperature.png

我将我的项目上传到 dropbox

推荐答案

您的项目问题出在资源文件夹中.在源文件夹之外找不到.

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:

在 NetBeans 中创建了一个 JavaFX 项目,并将资源文件夹移动到源文件夹中.所以这是源包:

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

然后将所有引用从 file: 更改为 "/":

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

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

和 css 文件:

.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;
}

现在它在桌面和 Raspberry Pi 中都可以正常工作.

Now it works fine in both desktop and Raspberry Pi.

这篇关于JavaFX 图像加载错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆