【发布时间】:2018-03-12 02:58:25
【问题描述】:
我似乎无法使用 Image 类。我和我的同事正在一起做一个项目,他似乎能够很好地运行所有代码而没有任何错误。当我尝试运行相同的代码并以相同的方式组织 src 时,我得到一个无效的 URL 或资源未找到错误。我在其他项目中遇到过这个问题,它似乎是特定于我的计算机或文件系统的。任何帮助将非常感激。这是代码示例。
public class HomePageGUI extends Application {
//Create the dispenser. Generates 5 Products in an array within the dispenser class.
private final Dispenser dispenser = new Dispenser();
//Get background image and add it to the stack pane
private final Image bgImage = new Image("/Dispenser Design/Background & Button Images/Background.png");
private final Button backButton = new Button("", new ImageView(
new Image("/Dispenser Design/Background & Button Images/Navigation Buttons/Back Button.png")));
private StackPane backgroundPane = new StackPane();
private Scene scene;
@Override
public void start(Stage primaryStage) {
primaryStage = homePage(primaryStage);
primaryStage.show();
}
//Display the home page
private Stage homePage(Stage stage) {
//IMAGE CREATION
Image drinksCategoryImage = new Image (
"/Dispenser Design/Background & Button Images/Starter Page/Drinks Button.png");
Image gumCategoryImage = new Image (
"/Dispenser Design/Background & Button Images/Starter Page/Gum Button.png");
Image sweetsCategoryImage = new Image (
"/Dispenser Design/Background & Button Images/Starter Page/Sweets Button.png");
//END IMAGE CREATION
//Create the buttons with the image displayed on them.
Button drinksCategoryButton = new Button(
"", new ImageView(drinksCategoryImage));
drinksCategoryButton.setOnAction(e -> {
drinksCategory(stage); });
Button gumCategoryButton = new Button(
"", new ImageView(gumCategoryImage));
gumCategoryButton.setOnAction(e -> {
gumCategory(stage); });
Button sweetsCategoryButton = new Button(
"", new ImageView(sweetsCategoryImage));
sweetsCategoryButton.setOnAction(e -> {
sweetsCategory(stage); });
//END BUTTON CREATION
【问题讨论】:
-
确认图像文件在您的硬盘上并且具有适当的权限。即,运行程序的用户可以读取图像。这个命令的输出是什么?
ls "/Dispenser Design/Background & Button Images/Background.png"? -
您传递的字符串显然是无效的网址(一方面,它们有空格)。
-
你好@Asaph 谢谢你的回复!我检查了我的文件权限,一切都是可读的。我还确保我以管理员身份运行 netbeans。要回答您的问题,该行代码用于创建稍后在窗格中使用的背景图像。那部分代码没有显示。
-
您好@James_D 感谢您的回复。我的同事和我在同一个 IDE 上运行这段完全相同的代码,他没有收到任何错误。空格在不同计算机上的作用会有所不同吗?
-
不确定:但它肯定不是有效的 URL。在任何计算机上将其捆绑为 jar 文件时,它肯定不起作用。