在 Javafx 中添加图像的最简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37111582/
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
Simplest way to add an image in Javafx?
提问by Asperger
I don't understand is how to add a simple image. I imported everything and followed what they said on this page:
我不明白是如何添加一个简单的图像。我导入了所有内容并按照他们在此页面上的说明进行操作:
http://www.java2s.com/Code/Java/JavaFX/LoadajpgimagewithImageanduseImageViewtodisplay.htm
http://www.java2s.com/Code/Java/JavaFX/LoadajpgimagewithImageanduseImageViewtodisplay.htm
Javafx code
Javafx 代码
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class test extends Application {
@Override
public void start(Stage stage) {
stage.setTitle("HTML");
stage.setWidth(500);
stage.setHeight(500);
Scene scene = new Scene(new Group());
VBox root = new VBox();
final ImageView selectedImage = new ImageView();
Image image1 = new Image(test.class.getResourceAsStream("C:\Users\user\Desktop\x.jpg"));
selectedImage.setImage(image1);
root.getChildren().addAll(selectedImage);
scene.setRoot(root);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Error
错误
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication6(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.scene.image.Image.validateInputStream(Image.java:1110)
at javafx.scene.image.Image.<init>(Image.java:694)
at prototype.test.start(test.java:23)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication13(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait6(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null4(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater5(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null9(WinApplication.java:191)
回答by pzaenger
Your problem seems to be this line:
你的问题似乎是这一行:
Image image1 = new Image(test.class.getResourceAsStream("C:\Users\user\Desktop\x.jpg"));
Because you get a NullPointerException: java.lang.NullPointerException: Input stream must not be null
.
因为你得到一个 NullPointerException: java.lang.NullPointerException: Input stream must not be null
。
See:
看:
回答by Numb
Your problem is at line getResourceAsStream("C:\\Users\\user\\Desktop\\x.jpg"));
Replace "\\" to "/", your problem is solved
Your problem is at line getResourceAsStream("C:\\Users\\user\\Desktop\\x.jpg"));
将“\\”替换为“/”,您的问题就解决了
回答by fabian
Class.getResourceAsStream
tries to load an element in the classpath. It's not meant to load files unless they are included in the classpath. To load a file outside of the classpath, use a FileInputStream
instead:
Class.getResourceAsStream
尝试加载类路径中的元素。除非它们包含在类路径中,否则它并不意味着加载文件。要在类路径之外加载文件,请改用 a FileInputStream
:
Image image1 = new Image(new FileInputStream("C:\Users\user\Desktop\x.jpg"));
Or use the Image(String)
constructorand pass the URL
of the file:
或者使用Image(String)
构造函数并传递URL
文件的:
Image image1 = new Image(new File("C:\Users\user\Desktop\x.jpg").toURI().toURL().toExternalForm());
回答by Charitha.R
Try adding image to GridPane
尝试将图像添加到 GridPane
ImageView img1 = new ImageView(new
Image(getClass().getResourceAsStream("C:\Users\user\Desktop\x.jpg")));
GridPane.setConstraints(img1, 0, 4);
回答by franciscoTavares
I think a good way to fix this is dragging your image to the project folder. That way you can search for the name alone
我认为解决这个问题的一个好方法是将您的图像拖到项目文件夹中。这样你就可以单独搜索名字了
Image image1 = new Image(test.class.getResourceAsStream("x.jpg"));
if this does'nt work add File:
如果这不起作用,请添加文件:
回答by Kr?w
As fabian mentioned, Class.getResourceAsStream(...)
loads an InputStream
from the classpath, (meaning, from the jar file itself). To load a file from your computer, you can simply use the file
protocol and give the path to the Image
constructor, like so:
正如 fabian 提到的,从类路径Class.getResourceAsStream(...)
加载一个InputStream
(意思是,从 jar 文件本身)。要从您的计算机加载文件,您可以简单地使用file
协议并提供Image
构造函数的路径,如下所示:
Image image1 = new Image("file:///C:/Users/user/Desktop/x.jpg");
The reason that you were getting:
你得到的原因:
Caused by: java.lang.NullPointerException: Input stream must not be null
in your error is because Class.getResourceAsStream(...)
simply returns null
when it can't find the resource that you tell it to look for. The Image
constructor was receiving null
and then throwing an exception.
在您的错误中是因为它Class.getResourceAsStream(...)
只是null
在找不到您告诉它要查找的资源时才返回。该Image
构造函数接收null
,然后抛出异常。