java - 为什么我在 JavaFX 的应用程序启动方法中出现异常
问题描述
我目前正在学习如何制作 GUI。我将此代码复制并粘贴到 Intellij IDEA 中。我有其他 GUI 程序可以工作,但到目前为止,这个特定的程序给了我这个错误。
我尝试查看有关此问题的其他帖子,其中很多似乎都在处理 FXML 元素。我只是困惑。
package sample;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.geometry.Insets;
import javafx.stage.Stage;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class ShowImage extends Application {
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
// Create a pane to hold the image views
Pane pane = new HBox(10);
pane.setPadding(new Insets(5, 5, 5, 5));
Image image = new Image("image/us.gif");
pane.getChildren().add(new ImageView(image));
ImageView imageView2 = new ImageView(image);
imageView2.setFitHeight(100);
imageView2.setFitWidth(100);
pane.getChildren().add(imageView2);
ImageView imageView3 = new ImageView(image);
imageView3.setRotate(90);
pane.getChildren().add(imageView3);
// Create a scene and place it in the stage
Scene scene = new Scene(pane);
primaryStage.setTitle("ShowImage"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
/**
* The main method is only needed for the IDE with limited
* JavaFX support. Not needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
我不断收到此错误。
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1107)
at javafx.graphics/javafx.scene.image.Image.<init>(Image.java:617)
at IDK/sample.ShowImage.start(ShowImage.java:18)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.graphics/javafx.scene.image.Image.validateUrl(Image.java:1099)
... 11 more
Exception running application sample.ShowImage
解决方案
在您发布的堆栈跟踪中查找最后一个Caused by,即
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
然后ShowImage
在堆栈跟踪中查找您的类,即
at IDK/sample.ShowImage.start(ShowImage.java:18)
我猜有问题的行是:
Image image = new Image("image/us.gif");
换句话说,“image/us.gif”不是图片的正确 URL。
你熟悉getResource()
课堂上的方法java.lang.Class
吗?它返回一个 URL,然后您可以使用该 URL 创建您的Image
.
推荐阅读
- javascript - jQuery.animate(...) 无法正常工作
- c - 如何通过使用 getchar 按下箭头键来避免阅读奇怪的符号?
- database - Linux服务器中PERL代码中的文件扩展名类型
- javascript - 带有 $.getJSON() 的 canvasJS 出现 CORB 错误
- python - 如何将一组中的非空值分配给熊猫组中的所有行?
- wolfram-mathematica - 无法在 wolfram-mathmica 中完成傅立叶分析?
- python - 如何处理 PyTorch 的 LSTM 模块中的可变大小序列?
- c++ - 无法在 C++ 中打印矢量对象数据成员值
- javascript - 在 Intersection Observer API 中定位目标
- amazon-web-services - AWS CloudFomation:无法与 ElasticBeanstalk 一起创建 RDS 资源