JavaFX 图像未在舞台上显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24260019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:10:31  来源:igfitidea点击:

JavaFX Image not showing in stage

javaimagejavafx-2javafx-8fxml

提问by Throoze

I've tried several times and several ways but I can't make my image show on stage as I want. I think it might has to do with the path where java looks for resources, but i'm not sure, since I'm just starting using visual libraries (JavaFX in this case). Here's my directory structure:

我已经尝试了很多次和几种方法,但我不能让我的形象在舞台上随心所欲地展现出来。我认为这可能与 java 查找资源的路径有关,但我不确定,因为我刚刚开始使用可视化库(在本例中为 JavaFX)。这是我的目录结构:

MyProject
 |_assets
 |  |_img
 |     |_myImage.jpg
 |
 |_some
 |_other
 |_folders
 |
 |_src
    |_ve
       |_org
          |_project
             |_MyProject.java
             |_StratPage.fxml
             |_StartPageController.java

I need to retreive myImage.jpgto be rendered, and I've tried the following:

我需要检索myImage.jpg才能呈现,并且我尝试了以下操作:

1) Pure fxml approach:

1)纯fxml方法:

<ImageView
     id="logo" 
     fx:id="logo"
     fitHeight="99.0" 
     fitWidth="99.0" 
     layoutX="14.0" 
     layoutY="18.0" 
     pickOnBounds="true" 
     preserveRatio="true">
         <image>
            <Image url="@../../../../assets/img/myImage.jpg" />
         </image>
</ImageView>
<ImageView
     id="logo" 
     fx:id="logo"
     fitHeight="99.0" 
     fitWidth="99.0" 
     layoutX="14.0" 
     layoutY="18.0" 
     pickOnBounds="true" 
     preserveRatio="true">
         <image>
            <Image url="@../../../../assets/img/myImage.jpg" />
         </image>
</ImageView>

2) Using both fxml and java. Declaring the ImageViewelement with fx:id="logo", and injecting the image from StartPageController.javalike this:

2) 同时使用 fxml 和 java。用 声明ImageView元素fx:id="logo",并StartPageController.java像这样注入图像:

public class StartPageController implements Initializable {

    @FXML
    private ImageView logo;


    @Override
    public void initialize(URL url, ResourceBundle rb) {
        this.logo = new ImageView("file:../../../../assets/img/myImage.jpg");
    }    

}

Neither way produce any exception, i just does not show the image. I have no idea what to do. I would really appreciate your help.

两种方式都不会产生任何异常,我只是不显示图像。我不知道该怎么做。我将衷心感谢您的帮助。

UPDATES:

更新:

First

第一的

I tried giving up on having the proposed directory structure, and placed the image file in the same folder of StartPageController.java. By doing

我尝试放弃建议的目录结构,并将图像文件放在StartPageController.java. 通过做

logo = new ImageView(new Image(getClass().getResourceAsStream("myImage.jpg")))

I'm not getting any exception, but the image is not rendering, which suggest me it's not about finding the resource, but about rendering the image. Could it be the lack of any library? I'm on a Windows 8 environment, using Netbeans 8.0. Thanks again for your answers.

我没有得到任何异常,但图像没有渲染,这表明我不是在寻找资源,而是渲染图像。可能是缺少任何图书馆吗?我在 Windows 8 环境中,使用 Netbeans 8.0。再次感谢您的回答。

Second

第二

I just deactivated packaging and distributing the app in the project properties in Netbeans. Now the images are rendering correctly, but I don't consider the issue solved, since when I need to distribute the software it will re emerge. Please, help is still needed! :)

我刚刚在 Netbeans 的项目属性中停用了打包和分发应用程序。现在图像渲染正确,但我认为问题没有解决,因为当我需要分发软件时,它会重新出现。请,仍然需要帮助!:)

采纳答案by Anatole ABE

I had the same error as you. So to correct it placed my image in resources folder in the same level of "src" of my project. This is my code, and it works.

我和你有同样的错误。因此,为了纠正它,将我的图像放在与我的项目的“src”相同级别的资源文件夹中。这是我的代码,它有效。

public void showImage() {
    try {
        Image image = new Image("resources/img/akonolingaMap.jpg");
        imageView.setImage(image);
        imageView.setCache(true);
    } catch (Exception e) {
        printStackTrace();
    }
}

回答by Joop Eggen

In a java application there is a distinction between file system Fileand resource(on the class path, inside the .jar).

在 java 应用程序中,文件系统File资源之间存在区别(在类路径上,在 .jar 中)。

If the image is part of the application, and fixed, read-only, as your relative path suggests, use a resource.

如果图像是应用程序的一部分,并且是固定的、只读的,如您的相对路径所示,请使用资源。

However then the image must be inside the jar.

但是,图像必须在罐子内。

Maybe:

也许:

/src/assets/img/myImage.jpg

or make /assets a top source directory too.

或使 /assets 也成为顶级源目录。

Then /img/myImage.jpg(or in the first case /assets/img/myImage.jpg)

然后/img/myImage.jpg(或在第一种情况下/assets/img/myImage.jpg

Programmatically one does not use File but URL getClass().getResource("/img/myImage.jpg")or getResourceAsStream.

以编程方式不使用 File 而是使用 URLgetClass().getResource("/img/myImage.jpg")getResourceAsStream.

回答by Perneel

I checked the code in my own project and it works with snippet below. I've adjusted it to your example

我检查了自己项目中的代码,它适用于下面的代码片段。我已将其调整为您的示例

 this.logo = new ImageView(new Image(getClass().getResourceAsStream("/assets/img/myImage.jpg")));

回答by PiBits

If you use Intellij as your main editor, then you could mark your folder as "Resources" or just open "Project Structure" -> "Modules" -> then mark your image folder as "Resources"

如果你使用 Intellij 作为你的主编辑器,那么你可以将你的文件夹标记为“Resources”或者只是打开“Project Structure”->“Modules”->然后将你的图像文件夹标记为“Resources”

Hope that will help you.

希望这会帮助你。