简体   繁体   English

为什么使用 javafx 时我的图像不可见?

[英]Why isn't my image visibile when using javafx?

Whenever I run it, it just shows the buttons under the space where the image should be, but there is no image even though the image space is there.每当我运行它时,它只显示图像应该在的空间下的按钮,但是即使图像空间在那里,也没有图像。

This is with intelliJ, I've tried many solutions but they aren't working.这是 intelliJ,我尝试了很多解决方案,但它们都不起作用。

First part of code第一部分代码

Second part of code第二部分代码

Files文件

Firstly, please use the code option of stack overflow to share code, so that others can copy, edit and run the code in their IDE.首先,请使用堆栈溢出的代码选项来共享代码,以便其他人可以在自己的 IDE 中复制、编辑和运行代码。

You have two options:你有两个选择:

Bad way: Try to access the image by:坏方法:尝试通过以下方式访问图像:

new Image("images/CheskePic.png");

Better way: Instead of saving the image in the src folder, create a resource folder and save the image there.更好的方法:不是将图像保存在src文件夹中,而是创建一个resource文件夹并将图像保存在那里。 IntelliJ should create such a folder automatically. IntelliJ 应该自动创建这样一个文件夹。 If not create a folder named 'resources', right-click on it 'Mark Directory as' and click on 'Resource Root'如果不创建名为“资源”的文件夹,请右键单击它“将目录标记为”,然后单击“资源根”

Then access the image by:然后通过以下方式访问图像:

new Image(getClass().getResourceAsStream("path/to/image.png")));

or:或者:

new Image(YourClass.class.getResourceAsStream("path/to/image.png")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM