【问题标题】:Why the program gets the error in JavaFX? [duplicate]为什么程序在 JavaFX 中出现错误? [复制]
【发布时间】:2021-01-19 22:56:24
【问题描述】:

过去 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);
    }
}

【问题讨论】:

  • 请不要截图文字..

标签: javafx


【解决方案1】:

尝试将您的图像移动到“资源”文件夹,如果需要,创建一个。

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

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

【讨论】:

  • 在类路径名称中包含“资源”看起来.. 可疑(尽管可能有一个包含它的 package 名称,但这很不寻常;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多