gpt4 book ai didi

java - 如何使用javafx设置图像

转载 作者:行者123 更新时间:2023-11-30 03:15:08 24 4
gpt4 key购买 nike

我在使用此代码设置图像时遇到错误。我还将图像放在 src 文件夹中。我尝试了其他方法,但对我没有任何作用。

Code:

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Clock extends Application{

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

}

@Override
public void start(Stage ps) throws Exception {
ps.setTitle("Analog Clock");
ps.show();

StackPane pane = new StackPane();

pane.setAlignment(Pos.CENTER);

ImageView image = new ImageView(new Image(getClass().getResourceAsStream("analog.png")));
pane.getChildren().add(image);

//Image img = new Image("analog.png");

// ImageView iv1 = new ImageView();
// iv1.setImage(img);

Scene scene = new Scene(pane);

每次运行代码时都会出现以下错误。

Error:


Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/14845382.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Input stream must not be null
at javafx.scene.image.Image.validateInputStream(Unknown Source)
at javafx.scene.image.Image.<init>(Unknown Source)
at Clock.start(Clock.java:26)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/2934105.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/18503843.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/19884472.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/2180324.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$145(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/3326003.run(Unknown Source)
... 1 more
Exception running application Clock

帮我解决这个问题。谢谢

最佳答案

Caused by: java.lang.NullPointerException: Input stream must not be null
...
at Clock.start(Clock.java:26)

第 26 行有一个 NPE,即以下代码行:

ImageView image = new ImageView(new Image(getClass().getResourceAsStream("analog.png")));

罪魁祸首可能是指analog.png。系统无法从您提供的位置读取文件,并且 getResourceAsStream() 返回 null 值。

您可以通过将行分成以下形式来进一步确定这一点:

InputStream inStream = getClass().getResourceAsStream("analog.png");
Image imageObject = new Image(inStream);
ImageView image = new ImageView(imageObject);

并确认运行 Clock 时新堆栈跟踪引用了哪个行号。

确认您的文件 analog.png 可以访问,或者更正图像文件沿路径的路径/位置。 this Stack Overflow Question 中总结了在返回 null 时正确使用 getResourceAsStream() 的规则。 .

关于java - 如何使用javafx设置图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32825846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com