创建图像变量不起作用
Creating image variable not working
所以我在eclipse中写了如下代码:
package myProgram;
import com.sun.glass.ui.Screen;
import com.sun.prism.paint.Color;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application implements EventHandler<ActionEvent> {
Button btn1;
Button btn2;
Button btn3;
public float[] winSize = {200, 400};
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
VBox layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
Scene scene = new Scene(layout, winSize[0], winSize[1]);
Rectangle2D rect = javafx.stage.Screen.getPrimary().getVisualBounds();
stage.setY(rect.getMinY() + rect.getHeight() - winSize[1]);
stage.setX(rect.getMinX() + rect.getWidth() - winSize[0]);
//Not working part below
Image imageDecline = new Image(getClass().getResourceAsStream("image.png"));
Button button5 = new Button();
button5.setGraphic(new ImageView(imageDecline));
//
btn1 = new Button("Close");
btn1.setOnAction(e -> stage.close());
btn1.setStyle("-fx-font: 20 arial");
layout.getChildren().add(btn1);
btn1.setMaxWidth(160);
System.out.print(System.getProperty("user.dir").toString());
btn2 = new Button("Open new Win");
btn2.setOnAction(e -> {
});
btn2.setStyle("-fx-font: 20 arial");
layout.getChildren().add(btn2);
btn2.setMaxWidth(160);
btn3 = new Button("Alert");
btn3.setOnAction(e -> {
});
btn3.setStyle("-fx-font: 20 arial");
layout.getChildren().add(btn3);
btn3.setMaxWidth(160);
stage.setScene(scene);
stage.show();
}
public void handle(ActionEvent event){
}
}
但是如果我删除这部分就可以了:
//Not working part below
Image imageDecline = new Image(getClass().getResourceAsStream("image.png"));
Button button5 = new Button();
button5.setGraphic(new ImageView(imageDecline));
//
我在控制台中收到以下文本,但程序无法打开:
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$launchApplication5(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 myProgram.Main.start(Main.java:45)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null3(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(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$null8(Unknown Source)
... 1 more
Exception running application myProgram.Main
有谁知道为什么当我尝试为 hold/reference(不确定)具有 .png 格式的图像创建变量时出现此问题?
我想加载图像,但它处于脱机状态。而且我无法理解如何从类似问题的答案中解决这个问题,我的文件对我来说似乎很不一样。
通常是请求的资源名称与现有资源不匹配。
特别是当没有提到 folder/package 时。 Eclipse 可以看到它自己的资源,但部署的应用程序可能没有它们。
消除这种歧义的简单解决方案是在您的资源中包含文件夹并直接使用此名称。
不是 image.png 而是 /images/image.png
我建议从根开始资源名称,以消除其他相关的混淆。
如果您仍然感到困惑,我建议您开始调查您的应用程序中有哪些资源。列出现有资源或尝试访问您认为在此处的资源。然后你的代码就会有异常,并且可以很容易地看出哪里出了问题。
所以我在eclipse中写了如下代码:
package myProgram;
import com.sun.glass.ui.Screen;
import com.sun.prism.paint.Color;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class Main extends Application implements EventHandler<ActionEvent> {
Button btn1;
Button btn2;
Button btn3;
public float[] winSize = {200, 400};
public static void main(String[] args){
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
VBox layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
Scene scene = new Scene(layout, winSize[0], winSize[1]);
Rectangle2D rect = javafx.stage.Screen.getPrimary().getVisualBounds();
stage.setY(rect.getMinY() + rect.getHeight() - winSize[1]);
stage.setX(rect.getMinX() + rect.getWidth() - winSize[0]);
//Not working part below
Image imageDecline = new Image(getClass().getResourceAsStream("image.png"));
Button button5 = new Button();
button5.setGraphic(new ImageView(imageDecline));
//
btn1 = new Button("Close");
btn1.setOnAction(e -> stage.close());
btn1.setStyle("-fx-font: 20 arial");
layout.getChildren().add(btn1);
btn1.setMaxWidth(160);
System.out.print(System.getProperty("user.dir").toString());
btn2 = new Button("Open new Win");
btn2.setOnAction(e -> {
});
btn2.setStyle("-fx-font: 20 arial");
layout.getChildren().add(btn2);
btn2.setMaxWidth(160);
btn3 = new Button("Alert");
btn3.setOnAction(e -> {
});
btn3.setStyle("-fx-font: 20 arial");
layout.getChildren().add(btn3);
btn3.setMaxWidth(160);
stage.setScene(scene);
stage.show();
}
public void handle(ActionEvent event){
}
}
但是如果我删除这部分就可以了:
//Not working part below
Image imageDecline = new Image(getClass().getResourceAsStream("image.png"));
Button button5 = new Button();
button5.setGraphic(new ImageView(imageDecline));
//
我在控制台中收到以下文本,但程序无法打开:
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$launchApplication5(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 myProgram.Main.start(Main.java:45)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication12(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait5(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null3(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater4(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$null8(Unknown Source)
... 1 more
Exception running application myProgram.Main
有谁知道为什么当我尝试为 hold/reference(不确定)具有 .png 格式的图像创建变量时出现此问题? 我想加载图像,但它处于脱机状态。而且我无法理解如何从类似问题的答案中解决这个问题,我的文件对我来说似乎很不一样。
通常是请求的资源名称与现有资源不匹配。 特别是当没有提到 folder/package 时。 Eclipse 可以看到它自己的资源,但部署的应用程序可能没有它们。
消除这种歧义的简单解决方案是在您的资源中包含文件夹并直接使用此名称。 不是 image.png 而是 /images/image.png
我建议从根开始资源名称,以消除其他相关的混淆。
如果您仍然感到困惑,我建议您开始调查您的应用程序中有哪些资源。列出现有资源或尝试访问您认为在此处的资源。然后你的代码就会有异常,并且可以很容易地看出哪里出了问题。