java - 我的代码出现错误,我不知道如何修复(Java fx stackPane)
问题描述
所以这是我的代码 这是一个非常简单的项目,但我认为某些模块无论出于何种原因都无法正确加载,这就是为什么我要向一般了解 javafx 和 java IDE 的人寻求帮助。我只是想打印一个带有一个图像和不同形状的stackPane,其中带有标记的文本。这段代码应该可以正常工作,我尝试了我发现的所有内容,但没有任何效果,我认为这是一个特例。我尝试过更改运行配置,更改 sdk,尽可能多地围绕项目结构进行播放,但没有消除此错误
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.scene.control.ContentDisplay;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Ellipse;
public class LabelWithGraphic extends Application {
@Override
public void start(Stage primaryStage) {
ImageView leb = new ImageView(new Image("lebanon.jpg"));
Label lb1 = new Label("Lebanon\n Our beloved country", leb);
lb1.setStyle("-fx-border-color: red; -fx-border-width: 4");
lb1.setContentDisplay(ContentDisplay.TOP);
lb1.setTextFill(Color.LIMEGREEN);
Label lb2 = new Label("Circle", new Circle(50, 50, 25));
lb2.setContentDisplay(ContentDisplay.BOTTOM);
lb2.setTextFill(Color.MAGENTA);
Label lb3 = new Label("Rectangle", new Rectangle(10, 10, 50, 25));
lb3.setContentDisplay(ContentDisplay.RIGHT);
Label lb4 = new Label("Ellipse", new Ellipse(50, 50, 50, 25));
lb4.setContentDisplay(ContentDisplay.LEFT);
Ellipse ellipse = new Ellipse(50, 50, 50, 25);
ellipse.setStroke(Color.AQUAMARINE);
ellipse.setFill(Color.WHITE);
StackPane stackPane = new StackPane();
stackPane.getChildren().addAll(ellipse, new Label("JavaFx"));
Label lb5 = new Label("A pane inside a label", stackPane);
lb5.setContentDisplay(ContentDisplay.BOTTOM);
HBox pane = new HBox(20);
pane.getChildren().addAll(leb, lb2, lb3, lb4, lb5);
Scene scene = new Scene(pane, 450, 150);
primaryStage.setTitle("Hi guys :D");
primaryStage.setScene(scene);
primaryStage.show();
}
}
这是我得到的错误:
Exception in thread "main" 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 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 LabelWithGraphic.start(LabelWithGraphic.java:19)
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
我对 java fx 还是比较陌生,我不知道我做错了什么。我尝试了许多解决方案,但没有一个解决方案,我也在使用 IntelliJ IDE,并且 javafx 没有内置,所以我不得不下载并手动添加它。任何帮助,将不胜感激
解决方案
将图像文件保存在资源文件夹中并使用以下代码。我试过了,效果很好。
InputStream is = getClass().getClassLoader().getResourceAsStream("lebanon.jpg");
ImageView leb = new ImageView(new Image(is));
推荐阅读
- vuejs3 - Vue3 - EventBus 的官方替代品是什么
- ruby-on-rails - 为什么“坏 URI(不是 URI?):http://www.example.com:80index”?
- python - 如果布尔值为真,如何仅运行 discord.py 命令
- javascript - 如何在 JavaSript 中的 3 个类之间切换?
- python - pySpark:如何在数据框中的 arrayType 列中获取 structType 中的所有元素名称?
- mfc - 使用CMFCPropertyGridCtrl,出现:派生类中的OnDestroy或PostNcDestroy将不会被调用
- java - Java Files.walk遇到异常错误后能否继续执行
- python - Pandas 将列熔化为行
- php - 本地主机上 Laravel 8 中的存储链接问题
- r - 在 R 中切换管道中 paste() 的顺序