当前位置: 首页 > 知识库问答 >
问题:

JavaFx添加图像

司寇灵均
2023-03-14

无论我做什么来改变它,我都会不断得到这个错误。我对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

当我添加一个图片的URL作为新图片时,这很好,但是当我有一个下载的图片时,它会给我这个错误。我的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

共有3个答案

司空瑾瑜
2023-03-14

更改以下行:

ImageView图像=new ImageView(new Image(SeaApp.class.getResourceAsStream("图像/space.png")))

ImageView图像=new ImageView(new Image(SeaApp.class.getResourceAsStream(文件:./"图像/space.png))

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

宗政颖逸
2023-03-14

这样做:

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());
...

并检查图像是否存在。

司徒炎彬
2023-03-14

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

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

编辑:

它应该很好用。

 类似资料:
  • 这是我的代码: controller.java main.java 当我运行我的项目并在文本字段中键入1并单击按钮时,它将显示图片。现在,在不关闭程序的情况下,我将一个2.png文件添加到res文件夹中。并在文本字段中键入2。点击该按钮后,它给我“java.lang.IllegalArgumentException:Invalid URL:Invalid URL或resource not Foun

  • 我不熟悉Java和OOP,一直在将图像添加到tableview列。代码似乎工作,我可以看到学生的名字正确,但图像没有显示在列中。我遇到了这个错误,无法理解如何使其工作: 学生模型: 和参与者场景,我有桌面视图: 如果可能有帮助,还可以添加讲座模型: 感谢您的帮助,谢谢!

  • 问题内容: 我想先在Javafx中为我的图像添加计时器,例如,大约3秒钟显示我的第一张图像,然后大约5秒钟显示我的第二张图像,此后什么也没有显示。对此有什么想法吗? 问题答案: 使用更新的:

  • 问题内容: 我正在尝试找到一种将图像添加到JavaFx TableView列的方法,该图像具有通过hibernate从H2数据库填充的其他列中的数据。TableView是在JavaFx Scene Builder中设计的。 到目前为止,这是我设法做到的: 控制器类: 我说那是一个错误。 这是我第一次尝试将图像添加到TableView中。 从昨天开始,我一直四处张望,但现在似乎被困住了。我希望能有所

  • 我试图找到一种方法,将图像添加到JavaFx TableView列中,该列包含通过hibernate从H2数据库填充的其他列中的数据。TableView是在JavaFx场景生成器中设计的。 到目前为止,我一直在努力总结: 控制器类: 我得到一个错误在说. 这是我第一次尝试将图像添加到TableView中。 我从昨天开始四处看看,但现在似乎被卡住了。我希望得到一些帮助。我非常感谢你的帮助。 这是创建

  • 我使用以下代码将图标添加到TreeItem中。使用Jfeniox库进行材质设计图标。当我点击tree项时,图标会随机出现在treeview列表的末尾,就像在Image中一样。