gpt4 book ai didi

JavaFX从变量路径加载图像

转载 作者:行者123 更新时间:2023-11-30 05:44:12 25 4
gpt4 key购买 nike

我正在尝试制作一个简单的骰子软件,它随机生成 1 到 6 之间的数字,并分别输出该状态下的骰子图像。

使用 Image 对象生成图像时,我在获取图像路径时遇到问题。如果我直接输入路径,它就可以工作,但是尝试使用变量会给我错误。

当我单击软件中的“随机”按钮时,会打印以下错误:

Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1118)
at javafx.scene.image.Image.<init>(Image.java:620)
at dicegui.DiceGUI.lambda$start$0(DiceGUI.java:83)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found
at javafx.scene.image.Image.validateUrl(Image.java:1110)
... 49 more

这是引发错误的代码:

        btnRand.setOnAction((ActionEvent e) ->
{
String imgPath = getImage();
System.out.println("\"" + imgPath + "\"");
Image img = new Image("\"" + imgPath + "\"");
//Image img = new Image("img/2.png"); I WAS JUST USING THIS TO TEST
grid.getChildren().add(new ImageView(img));
randOutput.setTextFill(Color.GREEN);
});

这是我的全部代码。值得注意的是,我使用的是 NetBeans。

package dicegui;


import java.util.Random;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
*
* @author josef
*/
public class DiceGUI extends Application
{
public int rollDice()
{
int value;
Random ran = new Random();

value = ran.nextInt(6) + 1;

return value;
}

public String getImage()
{
int value = rollDice() - 1;
String imageLocation;

String[] images = new String[6];
images[0] = "img/1.png";
images[1] = "img/2.png";
images[2] = "img/3.png";
images[3] = "img/4.png";
images[4] = "img/5.png";
images[5] = "img/6.png";

imageLocation = images[value];

return imageLocation;
}

@Override
public void start(Stage primaryStage)
{
primaryStage.setTitle("DiceGUI");

GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));


Button btnRand = new Button("Randomise");
GridPane.setHalignment(btnRand, HPos.CENTER);
grid.add(btnRand, 0, 0);

Label randOutput = new Label("Not yet randomised");
randOutput.setTextFill(Color.web("#CD2626"));
GridPane.setHalignment(randOutput, HPos.CENTER);
grid.add(randOutput, 0, 1);

btnRand.setOnAction((ActionEvent e) ->
{
String imgPath = getImage();
System.out.println("\"" + imgPath + "\"");
Image img = new Image("\"" + imgPath + "\"");
//Image img = new Image("img/2.png");
grid.getChildren().add(new ImageView(img));
randOutput.setTextFill(Color.GREEN);
});





Scene scene = new Scene(grid, 300, 300);
primaryStage.setScene(scene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}

}

文件结构如下:

file structure

最佳答案

您在字符串的开头和结尾使用双引号,这会产生无效的文件路径,例如“img/5.png”。有效路径为 img/5.png

只需使用imgPath加载图像:

Image img = new Image(imgPath);

或者,您可以使用类加载器来获取资源:

Image img = new Image(getClass().getResourceAsStream("/" + imgPath));

关于JavaFX从变量路径加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55131586/

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