gpt4 book ai didi

java - 我的代码中出现错误,但我不知道如何修复(Java fx stackPane)

转载 作者:行者123 更新时间:2023-12-01 17:50:08 24 4
gpt4 key购买 nike

这是我的代码这是一个非常简单的项目,但我认为某些模块由于某种原因无法正确加载,这就是为什么我向对 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));

关于java - 我的代码中出现错误,但我不知道如何修复(Java fx stackPane),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60812091/

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