使用 JavaFX 的骰子模拟器,图像未出现

Dice Simulator using JavaFX, images not appearing

我一直在尝试创建一个骰子模拟器,我将在其中创建一个 JavaFX 应用程序来模拟掷一对骰子。当用户单击一个按钮时,我的应用程序将生成两个随机数,每个都在 1 到 6 的范围内,以表示骰子的点数。然后,我的应用程序将使用 ImageView 控件显示骰子。

除了我的骰子图像不会出现之外,一切正常。我也没有收到任何错误,应用程序运行良好。我做错了什么,为什么我的骰子图像不出现?非常感谢任何帮助,谢谢大家。

DieRoll Class

import java.util.Random;

public class DieRoll 
{
    int rollNum;
    int sides = 6;
    
    public void roll()
    {
        Random rand = new Random();
        rollNum = rand.nextInt(sides) + 1;
    }
    
    public int getRoll()
    {
        return rollNum;
    }
}

DieImages Class

import javafx.scene.image.Image;

public class DieImages 
{
    Image pic1 = new Image("file:images/1Die.bmp");
    Image pic2 = new Image("file:images/2Die.bmp");
    Image pic3 = new Image("file:images/3Die.bmp");
    Image pic4 = new Image("file:images/4Die.bmp");
    Image pic5 = new Image("file:images/5Die.bmp");
    Image pic6 = new Image("file:images/6Die.bmp");
    
    private int value;
    private Image dieImage = pic1;
    
    public void setImage(int sides)
    {
        int value = sides;
        if(value == 1)
            dieImage = pic1;
        if(value == 2)
            dieImage = pic2;
        if(value == 3)
            dieImage = pic3;
        if(value == 4)
            dieImage = pic4;
        if(value == 5)
            dieImage = pic5;
        if(value == 6)
            dieImage = pic6;
    }
    
    public Image getImage()
    {
        return dieImage;
    }
    
}

骰子模拟器Class

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import java.util.Random;

public class DiceSimulator extends Application
{
     private ImageView viewDie1 = new ImageView();
     private ImageView viewDie2 = new ImageView();
     private Label resultDie;
     private Label message1;
     private Label message2;
     
     public static void main(String[] args)
       {
          // Launch the application.
          launch(args);
       }
       
       @Override
       public void start(Stage primaryStage)
       {
           primaryStage.setTitle("Dice Simulator");
           Label message1 = new Label("Welcome to the Dice Simulator!");
           Label message2 = new Label("Please hit start to roll the dice!");
           
           resultDie = new Label();
           Button startButton = new Button("Start!");
           
           startButton.setOnAction(new StartButtonHandler()); 
           
           HBox hbox = new HBox(10, viewDie1, viewDie2);
           
           VBox vbox = new VBox(10, message1, message2, startButton, hbox, resultDie);
           vbox.setPadding(new Insets(15));
           vbox.setAlignment(Pos.CENTER);
           
           Scene simulatorScene = new Scene(vbox);
           primaryStage.setScene(simulatorScene);
           primaryStage.show();
           
           
       }
       
       class StartButtonHandler implements EventHandler<ActionEvent>
       {
           @Override
           public void handle(ActionEvent event)
           {
               int num1 = 0;
               int num2 = 0;
               Image diePic1; 
               Image diePic2;

               DieRoll dieI = new DieRoll();
               DieRoll dieII = new DieRoll();
               dieI.roll();
               dieII.roll();
               num1 = dieI.getRoll();
               num2 = dieII.getRoll();
               
               DieImages image1 = new DieImages();
               DieImages image2 = new DieImages();
               image1.setImage(num1);
               image1.setImage(num2);
               diePic1 = image1.getImage();
               diePic2 = image2.getImage();
               
               viewDie1 = new ImageView(diePic1);
               viewDie2 = new ImageView(diePic2);
               
               resultDie.setText("You rolled a " + num1 + " and " + num2 + "!");
           }
       }
       
         
}

Java 找不到您要显示的文件,为什么 JavaFX 不抛出错误,没有线索。

相对位置(如您现在指定的那些)从 java 文件开始,如果 运行 文件本身,或者从项目根开始,如果 运行 文件通过 ide.

出于测试目的,请尝试使用绝对位置指定文件位置,例如 /home/user/images/1Die.bmp 代表 Linux 或 C:\Users\user\imagesDie.bmp 代表 Windows