为什么程序会在JavaFX中出现错误?

时间:2020-10-05 04:25:55

标签: javafx

过去3天,我一直在寻找答案,观看视频和论坛帖子,但没有运气。请看看那些4图片。看来只有在问题中有图像文件时,它才会这样做。

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;

public class Exercise14_02 extends Application {
    @Override
    public void start(Stage primaryStage) {
        Image imageX = new Image("image/x.gif");
        Image imageO = new Image("image/o.gif");

        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        pane.setHgap(5);
        pane.setVgap(5);

        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                int status = (int)(Math.random() * 3);
                if (status == 0) {
                    pane.add(new ImageView(imageX), j, i);     
                } else if (status == 1) {
                    pane.add(new ImageView(imageO), j, i);     
                }        
            }
        }

        // Create a scene and place it in the stage
        Scene scene = new Scene(pane);
        primaryStage.setTitle("Exercise14_02"); // Set the stage title
        primaryStage.setScene(scene); // Place the scene in the stage
        primaryStage.show(); // Display the stage
    }

    public static void main(String[] args) {
        launch(args);
    }
}

error

pic2

pic3

pic4

1 个答案:

答案 0 :(得分:2)

尝试将图像移到“资源”文件夹,并根据需要创建一个。

Image imagex = new Image(getClass().getResourceAsStream("/resources/your_image.jpg"));

或 检查您的资源文件夹是否已包含在项目中。