【问题标题】:Invalid Image url图片网址无效
【发布时间】: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 文件时,它肯定不起作用。

标签: java image javafx


【解决方案1】:

好的,经过数小时的故障排除后,我发现我需要将 file: 添加到图像路径。我不确定为什么我的同事不需要添加 file: 扩展名来使图像正常工作,但这就是它最终为我工作的方式。

    productsForSale[ 4] = new Gum("Wriggleys", "Big Red", .75, 1, new Image(
      "file:src/DispenserDesign/IndividualProducts/BigRedGum.jpg"));

【讨论】:

  • 但是,如果/当您将应用程序捆绑为 jar 文件时,这将不起作用,因为图像将不再位于其自己的文件中。而且在运行时引用 source 目录确实没有意义;不保证会可用。
  • @James_D 你有什么建议。如果你要制作一个图像,你会怎么做。如果你能写出一段代码给我看一个很棒的例子。
  • 如果它是项目的一部分,您应该获取 URL 作为资源,然后在 URL 上调用 toExternalForm()(因为 Image 构造函数需要字符串形式的 URL)。或许可以看一下stackoverflow.com/questions/34755630/…,它是关于获取 FXML 文件的正确 URL,但想法是一样的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-13
  • 1970-01-01
  • 2014-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多