编码新手,无法使其发挥作用

时间:2017-12-13 19:36:33

标签: javafx

我正在尝试在javafx中创建一个掷骰子的GUI。程序应该显示带按钮的骰子,当点击按钮时,它应该改变相应随机数的图像。我不知道如何让我的按钮工作。下面是我写的代码和我的Die类:

public class Die {

   protected int sideShowing;

   public Die() {

   }

   public void roll() {
      sideShowing = (int)(Math.random()*6) + 1;
   }

   public int getValue() {
      return sideShowing;
   }

   @Override
   public String toString(){
      switch(sideShowing){
         case 1: return "One";
         case 2: return "Two";
         case 3: return "Three";
         case 4: return "Four";
         case 5: return "Five";
         case 6: return "Six";
         default: return "No side";
      }
   }

 @Override
   public boolean equals(Object o){
      if(o instanceof Die){
         return this.getValue() == ((Die)o).getValue();
      }else{
         return false;
      }
   }

}
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.scene.image.ImageView;
import javafx.scene.image.Image;
import javafx.scene.text.Text;
import javafx.geometry.Pos;
import javafx.scene.layout.VBox;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.control.ComboBox;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;

public class DiceRoll extends Application{
   //add the start method
   @Override
   public void start(Stage primaryStage){

   Rectangle r1 = new Rectangle(300, 200);
   r1.setFill(Color.WHITE);

   Die die1 = new Die();
   Die die2 = new Die();

   die1.getValue();
   die2.getValue();

   Image image = new Image("Dice1.png");
   ImageView Dice1 = new ImageView(image);
      Dice1.setFitHeight(50);
      Dice1.setFitWidth(50);

   Image image2 = new Image("Dice2.png");
   ImageView Dice2 = new ImageView(image2);
      Dice2.setFitHeight(50);
      Dice2.setFitWidth(50);

   Image image3 = new Image("Dice3.png");
   ImageView Dice3 = new ImageView(image3);
      Dice3.setFitHeight(50);
      Dice3.setFitWidth(50);

   Image image4 = new Image("Dice4.png");
   ImageView Dice4 = new ImageView(image4);
      Dice4.setFitHeight(50);
      Dice4.setFitWidth(50);

   Image image5 = new Image("Dice5.png");
   ImageView Dice5 = new ImageView(image5);
      Dice5.setFitHeight(50);
      Dice5.setFitWidth(50);

   Image image6 = new Image("Dice6.png");
   ImageView Dice6 = new ImageView(image6);
      Dice6.setFitHeight(50);
      Dice6.setFitWidth(50);

   VBox vBox = new VBox(50);
   vBox.setAlignment(Pos.BASELINE_LEFT);


   Button btn = new Button();
    btn.setText("Roll Die!");
    btn.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
             if(die1.toString() == "one"){
               vBox.getChildren().add(Dice1);

             }
             else if(die1.toString() == "two"){
               vBox.getChildren().add(Dice2);

             }
             else if(die1.toString() == "three"){
               vBox.getChildren().add(Dice3); 

             }
             else if(die1.toString() == "four"){
               vBox.getChildren().add(Dice4);

             }
             else if(die1.toString() == "five"){
               vBox.getChildren().add(Dice5);

             }
             else if(die1.toString() == "six"){
               vBox.getChildren().add(Dice6);

             }
        }
    });  

   StackPane sPane = new StackPane();
   sPane.getChildren().add(vBox);

   VBox vBox2 = new VBox(50);
   vBox2.setAlignment(Pos.BOTTOM_CENTER);
   vBox2.getChildren().add(sPane); //Dice

   StackPane sPane2 = new StackPane();
   sPane2.getChildren().add(r1);
   sPane2.getChildren().add(sPane);
   sPane2.getChildren().add(vBox2);

   VBox vBox3 = new VBox(20);
   vBox3.setAlignment(Pos.BOTTOM_RIGHT);
   vBox3.getChildren().addAll(btn);

   FlowPane fPane = new FlowPane(20, 20);
   fPane.getChildren().addAll(vBox, sPane2, vBox3);


   Scene scene = new Scene(fPane, 300, 400);

   primaryStage.setTitle("JavaFX Example");
   primaryStage.setScene(scene);
   primaryStage.show();   

   }

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


   }

}

非常感谢任何帮助或建议,谢谢。

0 个答案:

没有答案