当前位置: 首页 > 知识库问答 >
问题:

BlackJack javafx命中按钮不工作

柳钟展
2023-03-14

我在21点程序中无法让点击按钮正常工作。我尝试为setOnAction块中的hit按钮以及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个答案

段良弼
2023-03-14
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

顺便说一句,我建议您阅读一下Java / Javafx的代码约定,因为它们通过提供标准样式来帮助其他程序员阅读您的代码。

洪俊捷
2023-03-14

您声明此全局

public int deck[];

然后是本地的

int[] deck = new int[52];

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

解决方案:

deck = new int[52];

其他变量也一样。

 类似资料:
  • 我面临commandButton的问题,它只有在类型为submit时才起作用。有没有人可以看一下,让我知道是否有解决办法?下面的代码非常简单,并有足够的建议来说明我需要什么。方法test()未执行。方法runSubmit已成功执行。 我需要测试方法在没有提交的情况下执行,因为原始页面确实在提交期间执行了验证,test()方法必须在没有提交的情况下执行,因为这是提交之前的一个初步操作。 我正在使用P

  • 所以我在我的一个程序中使用cardLayout,我试图使它成为当你点击一个按钮时下一个面板加载。我有一个panelHolder类,其中保存了cardlayout,每次按下面板上的按钮时,它都会调用panelHolder类中的一个方法,该方法根据按钮将某个布尔变量设置为true,并调用repaint(其中显示了面板)。由于某种原因,我的按钮坏了,我似乎找不出原因。有人能帮帮我吗?

  • 我有一个问题使添加到购物车按钮工作。不确定是哪一部分代码导致了这个问题。但是,问题的链接是:https://barkerbespoke.com/collections/tie-collection/products/black-wool 你有什么建议?

  • 我想使用一个p:对话框,其中包含一些commandbutton,但我不能从命令按钮调用我的bean,也不知道为什么。 在xhtml页面上有两个表单,第一个表单包含一些内容,如面板、panelgrids、按钮等。第二个表单只包含我的对话框。 我的beanHandler(beanHandler)是ViewAccessScoped。此xhtml页面上的所有内容都在工作,只有我的p:面板不工作 如果我使用

  • 我是Python新手,对GUI编程更是新手。 我有一个按钮和两个旋转箱,我想在点击开始按钮后禁用它们。我在谷歌上搜索了5种禁用Tkinter按钮的不同方法,但似乎都不起作用。理论上,旋转箱应该以同样的方式被禁用,但我只是运气不好。对整个图形用户界面感到非常沮丧。 和是两个spinbox 正如您所看到的,我尝试对按钮使用,但它不起作用。我见过各种各样的代码,在所有的大写字母、大写字母和不同的引号中都

  • 我一直在写一个小的登录servlet。登录部分工作得很好,但当我按下logout submit按钮时--什么也没有发生。 Servlet代码下面是: 有谁能帮我找出出了什么问题吗?我是JSP和Java servlet的新手。