JavaFx添加图像

时间:2015-06-08 21:46:26

标签: java image exception javafx imageview

无论我做什么改变它,我都会收到这个错误。我是JavaFX的新手,我想我会尝试一个小程序。我只是想在场景中显示一张图片,但我甚至无法在我的程序中加载图像。我将图像放在主包(seaapp)旁边的包(seaapp.images)中

package seaapp;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class SeaApp extends Application {

@Override
public void start(Stage primaryStage) {
    ImageView image = new ImageView(new Image(SeaApp.class.getResourceAsStream("images/space.png")));
    Pane root = new Pane();
    root.getChildren().add(image);
    Scene scene = new Scene(root, 300, 250);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

public static void main(String[] args) {
    launch(args);
}

}

这给了我这个错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/355629945.run(Unknown Source)
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:1109)
at javafx.scene.image.Image.<init>(Image.java:694)
at seaapp.SeaApp.start(SeaApp.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/1175241703.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1685538367.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/435396101.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/485815673.run(Unknown Source)
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$null$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/1673605040.run(Unknown Source)
... 1 more
Exception running application seaapp.SeaApp
Java Result: 1

当我将图片的网址添加为新图片时,这可以正常工作,但是当我有下载的图片时,它会给我这个错误。我的netbeans也完全更新了。我已经链接到我如何设置文件的屏幕截图。我错过了什么吗?

项目设置:

https://drive.google.com/file/d/0B_KZLduvxt26dE5XS1p5blE5VFE/view?usp=sharing

文件设置:

https://drive.google.com/file/d/0B_KZLduvxt26Nm1sZWROSlRMeUE/view?usp=sharing

链接到整个项目:

https://drive.google.com/file/d/0B_KZLduvxt26dXVFYlFhREFGZW8/view?usp=sharing

4 个答案:

答案 0 :(得分:2)

/之前添加正斜杠images/space.png,您的代码将正常工作(即,如果路径正确)。

ImageView image = new ImageView(new Image(SeaApp.class.getResourceAsStream("/images/space.png")));

修改

它应该可以正常工作。

Screenshot

答案 1 :(得分:0)

这样做:

Image image = new Image( getClass().getResource( "images/space.png").toExternalForm());
ImageView image = new ImageView( image);

如果这不起作用,请找出要查看的路径:

System.out.println( "Path: " + getClass().getResource("/").toExternalForm());
System.out.println( "Path: " + getClass().getResource("images").toExternalForm());
...

并检查图像是否存在。

答案 2 :(得分:0)

更改以下行:

ImageView image = new ImageView(新图片(SeaApp.class.getResourceAsStream(“images / space.png”)))

ImageView image = new ImageView(新图片(SeaApp.class.getResourceAsStream(“file:./”+“images / space.png”)))

参考:https://stackoverflow.com/a/23878479/6336264

答案 3 :(得分:0)

包含在exe中的jar里面的图像有同样的问题 问题是文件名中的第一个大写字母。路径在包装器中区分大小写,但在Windows上的IDE下运行时则不行。