每次我尝试添加图像时,JavaFX中总是出现错误。java.lang.reflect.InvocationTargetException
在过去的几天里,我尽了一切努力在JavaFx上获取图像。无论我尝试什么,都是同样的错误。无论我做什么,都是同样的错误。此错误仅在图像中发生。当我使用try-catch时,打印出的异常是java.lang.IllegalArgumentException:无效URL:无效URL或未找到资源每次我尝试添加图像时,JavaFX中总是出现错误。java.lang.reflect.InvocationTargetException,java,Java,在过去的几天里,我尽了一切努力在JavaFx上获取图像。无论我尝试什么,都是同样的错误。无论我做什么,都是同样的错误。此错误仅在图像中发生。当我使用try-catch时,打印出的异常是java.lang.IllegalArgumentException:无效URL:无效URL或未找到资源 package application; import java.io.FileInputStream; import java.io.FileNotFoundException; import j
package application;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
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.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class Practice extends Application {
public void start(Stage stage) throws FileNotFoundException {
//Creating an image
Image image = new Image(new
FileInputStream("Users/user/Documents/Minesweeper/1.png"));
//Setting the image view
ImageView imageView = new ImageView(image);
//Setting the position of the image
imageView.setX(50);
imageView.setY(25);
//setting the fit height and width of the image view
imageView.setFitHeight(455);
imageView.setFitWidth(500);
//Setting the preserve ratio of the image view
imageView.setPreserveRatio(true);
//Creating a Group object
Group root = new Group(imageView);
//Creating a scene object
Scene scene = new Scene(root, 600, 500);
//Setting title to the Stage
stage.setTitle("Loading an image");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]) {
launch(args);
}
}检查图像文件的路径。路径
“Users/user/Documents/mineswipper/1.png”
是一个相对路径,请确保Users
上方的目录是您当前的工作目录
否则,您可以将图像的绝对路径作为FileInputStream
的参数
简单的java文件结构应如下所示:
WorkingDir
|
----application
| |
| ----Practice.java
|----1.png
Image image = new Image(new FileInputStream("1.png"));