使用JavaFX的骰子模拟器

时间:2019-11-17 00:18:43

标签: java user-interface javafx simulator dice

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

当我尝试运行当前拥有的代码时,出现此错误:

Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Children: child node is null: parent = HBox@60b7c6fe
    at javafx.scene.Parent$2.onProposedChange(Parent.java:435)
    at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:234)
    at com.sun.javafx.collections.VetoableListDecorator.addAll(VetoableListDecorator.java:103)
    at javafx.scene.layout.HBox.<init>(HBox.java:261)
    at DiceSimulator.start(DiceSimulator.java:42)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    ... 1 more
Exception running application DiceSimulator

我不知道出了什么问题,因为一切对我来说都很好。感谢您的任何帮助,谢谢!

DieImages类

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;
    }

}

DieRoll类

import java.util.Random;

public class DieRoll 
{
    int rollNum;

    public void roll()
    {
        Random rand = new Random(6);
        rollNum = rand.nextInt();
    }

    public int getRoll()
    {
        return rollNum;
    }
}

骰子模拟器类

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;
     private ImageView viewDie2;
     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, 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();
               num1 = dieI.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 + "!");
           }
       }     
}

0 个答案:

没有答案