BlackJack javafx点击按钮不起作用

时间:2015-08-13 13:00:09

标签: button javafx blackjack

我无法在我的BlackJack程序中使用Hit按钮。我尝试编写setOnAction块中的命中按钮以及lambda的动作,但是我得到关于变量不是最终的错误。所以,我这样试过,但我不认为我的变量会继续存在。它可能非常简单。如果可以的话请帮忙。谢谢!

import java.util.Arrays;
import java.util.ArrayList;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.GridPane;
import javafx.scene.control.Label;
import javafx.scene.control.Labeled;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;


public class BlackJackGame extends Application {
    public String btHitY;
    public int NumberPlayerCards;
    public int NumberDealerCards;
    public int NUMBER_OF_CARDS;
    public int PlayerCards[];
    public int DealerCards[];
    public Image imagesP[];
    public Image imagesD[];
    public int deck[];
    public String URLBase;
    public GridPane pane;

    public BlackJackGame() {
        this.btHitY = " ";
        this.btHitY = new String();
    }

  @Override // Override the start method in the Application class
  public void start(Stage primaryStage) {

    //Create array deck, suit string, and rank string
    int[] deck = new int[52];
    String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
    String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
    int NUMBER_OF_CARDS = 4;
    String URLBase = "http://www.cs.armstrong.edu/liang/common/image/card/";

    //Initialize the cards
    for (int i = 0; i < deck.length; i++)
      deck[i] = i;

    //Shuffle the cards
    for (int i = 0; i < deck.length; i++) {
      //Generate an index randomly
      int index = (int)(Math.random() * deck.length);
      int temp = deck[i];
      deck[i] = deck[index];
      deck[index] = temp;
    }

    int NumberPlayerCards = 2;
    int NumberDealerCards = 2;
    int[] PlayerCards = new int[50];
    int[] DealerCards = new int[50];

    //Display the first cards
    for (int i = 0; i < 8; i++) {
      String suit = suits[deck[i] / 13];
      String rank = ranks[deck[i] % 13];
      System.out.println("Card number " + deck[i] + ": " + rank + " of " + suit);
    }

    for (int i = 0; i < NumberPlayerCards; i++) 
      PlayerCards[i] = deck[i * 2];

    for (int i = 0; i < NumberDealerCards; i++)
      DealerCards[i] = deck[i * 2 + 1];

    // Create a pane to hold the image views
    GridPane pane = new GridPane();
    pane.setAlignment(Pos.CENTER);
    pane.setPadding(new Insets(5, 5, 5, 5));
    pane.setHgap(5);
    pane.setVgap(5);
    Image[] imagesP = new Image[50];
    Image[] imagesD = new Image[50];

   for (int i = 0; i < NumberPlayerCards; i++) {
      int cardForPrint = PlayerCards[i] + 1;
      System.out.println(URLBase + cardForPrint + ".png");
      imagesP[i] = new Image(URLBase + cardForPrint + ".png");
    }

    for (int i = 0; i < NumberDealerCards; i++) {
      int cardForPrint = DealerCards[i] + 1;
      System.out.println(URLBase + cardForPrint + ".png");
      imagesD[i] = new Image(URLBase + cardForPrint + ".png");
    }
      //rotate flag image to cover dealer card
      Image flag = new Image("http://www.cs.armstrong.edu/liang/common/image/us.gif");
      ImageView imageFlag = new ImageView(flag);
      imageFlag.setRotate(90);
      imageFlag.setFitHeight(75);
      imageFlag.setFitWidth(95);

      pane.add(new Label("Player Cards"), 0, 0);
      pane.add(new ImageView(imagesP[0]), 1, 0);
      pane.add((imageFlag), 1, 1);
      pane.add(new Label("Dealer Cards"), 0, 1);
      pane.add(new ImageView(imagesP[1]), 2, 0);
      pane.add(new ImageView(imagesD[1]), 2, 1);  
      Button btHit = new Button("Hit");
      Button btStay = new Button("Stay");
      pane.add(btHit, 1, 2);
      pane.add(btStay, 2, 2);
      // Create a scene and place it in the stage
      Scene scene = new Scene(pane, 1200, 700);
      primaryStage.setTitle("Black Jack"); // Set the stage title
      primaryStage.setScene(scene); // Place the scene in the stage
      primaryStage.show(); // Display the stage
      HitHandlerClass handlerHit = new HitHandlerClass();
      btHitY = " ";
      btHit.setOnAction(handlerHit);

      /* if (btHitY.equals("Hit")); {
          NumberPlayerCards = NumberPlayerCards + 1;
          NUMBER_OF_CARDS = NUMBER_OF_CARDS + 1;
          PlayerCards[NumberPlayerCards - 1] = deck[NUMBER_OF_CARDS - 1];
          for (int j = 0; j < NumberPlayerCards; j++){
            System.out.println(PlayerCards[j]);
          }
          System.out.println(NumberPlayerCards);
          int CardForPrint2 = PlayerCards[NumberPlayerCards - 1] + 1;
          imagesP[NumberPlayerCards - 1] = new Image(URLBase + CardForPrint2 + ".png");          
          pane.add(new ImageView(imagesP[NumberPlayerCards - 1]), NumberPlayerCards, 0);
          btHitY = " ";
          primaryStage.show();
      } */

  }
  /**
   * The main method is only needed for the IDE with limited
   * JavaFX support. Not needed for running from the command line.
     * @param args
   */
   public static void main(String[] args) {
    launch(args);
   } 

   class HitHandlerClass implements EventHandler<ActionEvent> {
   @Override
        public void handle(ActionEvent e) {
          NumberPlayerCards = NumberPlayerCards + 1;
          NUMBER_OF_CARDS = NUMBER_OF_CARDS + 1;
          PlayerCards[NumberPlayerCards - 1] = deck[NUMBER_OF_CARDS - 1];
          for (int j = 0; j < NumberPlayerCards; j++){
            System.out.println(PlayerCards[j]);
          }
          System.out.println(NumberPlayerCards);
          int CardForPrint2 = PlayerCards[NumberPlayerCards - 1] + 1;
          imagesP[NumberPlayerCards - 1] = new Image(URLBase + CardForPrint2 + ".png");          
          pane.add(new ImageView(imagesP[NumberPlayerCards - 1]), NumberPlayerCards, 0);
          btHitY = " ";
        }
    }

}

2 个答案:

答案 0 :(得分:1)

您声明此全局

public int deck[];

然后是本地

int[] deck = new int[52];

所以全局永远不会被初始化并给你一个Nullpointer异常。

解决方案:

deck = new int[52];

其他变量也一样。

答案 1 :(得分:0)

public class BlackJackGame extends Application {
//snip
    public int NUMBER_OF_CARDS;
    public int deck[];
//snip

  @Override // Override the start method in the Application class
  public void start(Stage primaryStage) {

    //Create array deck, suit string, and rank string
    int[] deck = new int[52];
    int NUMBER_OF_CARDS = 4;
   //snip

您已在更高的范围内声明了deck []和NUMBER_OF_CARDS,然后在较低的范围内重新声明它们。

要解决这些问题,只需删除start方法中的类型声明即可。

  @Override // Override the start method in the Application class
  public void start(Stage primaryStage) {

    //Create array deck, suit string, and rank string
    deck = new int[52];
    NUMBER_OF_CARDS = 4;
   //snip

作为旁注,我建议您在Code conventions上阅读Java / Javafx,因为它们可以帮助其他程序员通过提供标准样式来读取代码。