【问题标题】:Cannot load image in JavaFX无法在 JavaFX 中加载图像
【发布时间】:2013-04-12 12:58:06
【问题描述】:

我测试了这段代码以创建带有图像的对话框。

final int xSize = 400;
final int ySize = 280;
final Color backgroundColor = Color.WHITE;
final String text = "SQL Browser";
final String version = "Product Version: 1.0";

final Stage aboutDialog = new Stage();
aboutDialog.initModality(Modality.WINDOW_MODAL);

Button closeButton = new Button("Close");

closeButton.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent arg0) {
        aboutDialog.close();
    }
});

GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));

Image img = new Image("logo.png");
ImageView imgView = new ImageView(img);

grid.add(imgView, 0, 0);

grid.add(new Text(text), 0, 1);
grid.add(new Text(version), 0, 2);
grid.add(closeButton, 14, 18);

Scene aboutDialogScene = new Scene(grid, xSize, ySize, backgroundColor);
aboutDialog.setScene(aboutDialogScene);
aboutDialog.show();

我将图像文件放入目录/src。 但由于某种原因,图像没有显示。你能帮我改正我的错误吗?

【问题讨论】:

标签: java javafx-2 javafx


【解决方案1】:

只需替换此代码:

Image img = new Image("logo.png");

有了这个

Image img = new Image("file:logo.png");

文档参考。 https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html

当您将String 传递给Image 类时,它可以通过四种不同的方式从文档中复制)进行处理:

// The image is located in default package of the classpath
Image image1 = new Image("/flower.png");

// The image is located in my.res package of the classpath
Image image2 = new Image("my/res/flower.png");

// The image is downloaded from the supplied URL through http protocol
Image image3 = new Image("http://sample.com/res/flower.png");

// The image is located in the current working directory
Image image4 = new Image("file:flower.png");

file: 前缀只是一个 URI 方案,或者换句话说,是 http: 协议分类器的对应物。这也适用于文件浏览器或网络浏览器... ;)

如需进一步参考,您可以查看文件 URI 方案的 wiki 页面:https://en.wikipedia.org/wiki/File_URI_scheme

快乐编码,

卡拉什

【讨论】:

  • "file:" 部分有什么作用?
  • 我已经编辑了我的答案,现在我希望它清楚它的作用。 :)
  • 奇怪的是 Schildt 没有在 Java 中解释这一点:完整参考
  • 我使用 Image(url);加载 1MB 大小的图像,需要 5 秒,太长了!有什么方法可以解决这个问题吗?
  • @LeeRQ:这是什么类型的 URL? HTTP 还是本地文件?我现在还没有遇到这个问题。
【解决方案2】:
Image img = new Image("file:/logo.png");

或带路径的方式:

Image img = new Image("file:c:/logo.png");

File f = new File("c:\\logo.png");
Image img = new Image(f.toURI().toString());

也可以使用:

new Image(file:src/logo.png) //root of project

【讨论】:

    【解决方案3】:

    将图像复制并粘贴到源包(NetBeans IDE 中的源包)所在的文件夹中。那么

    Image image = new Image("a1.jpg");
    Image image = new Image("File:a1.jpg");
    

    两者都可以。

    【讨论】:

      【解决方案4】:

      这个功能:

      Image image  = new Image(getClass()
              .getResourceAsStream("ChimpHumanHand.jpg"));
      

      【讨论】:

      • 获取任意目录下的图片:
      【解决方案5】:

      试试这个:

      img = new Image("/logo.png");
      

      如果没有给出指示 URL 的协议部分(如 http:file:),则该文件应该位于默认包中。如果您希望它放入不同的包中,例如 com.my.images,您可以以类似路径的方式添加此信息:

      img = new Image("/com/my/images/logo.png");
      

      【讨论】:

        猜你喜欢
        • 2019-05-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-08
        相关资源
        最近更新 更多