javafx选项框未获取项

qmelpv7a  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(263)

**结束。**此问题需要详细的调试信息。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

8天前关门了。
改进这个问题
我有一个程序可以在某些场景之间切换。有一个特别的选项似乎不适用于选项框。
主要方法如下:

public void start(Stage primaryStage)
    {
        window = primaryStage;
        window.setTitle("KTANE Bot by Hawker");
        window.setScene(PokerScene.getPokerScene());
        window.show();
    }

以下是由于某些原因似乎不起作用的方法:

import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import sample.Alerts.AlertBox;
import sample.CommonMethods;
import sample.Main;
import sample.Modules.Poker;

import static sample.Modules.Poker.Number.*;
import static sample.Modules.Poker.Suit.*;

public class PokerScene
{
    private static ChoiceBox<Poker.Number> firstCardNumberChoiceBox;
    private static HBox firstCardHBox;

    private static String response;

    private static Poker.Card firstCard;

    private static Poker.Suit card1Suit;
    private static Poker.Suit card2Suit;
    private static Poker.Suit card3Suit;
    private static Poker.Suit card4Suit;

    private static int bettingAmount;

    public static Scene getPokerScene()
    {
        firstCardNumberChoiceBox = new ChoiceBox<>();
        firstCardNumberChoiceBox.getItems().addAll(TWO, FIVE, KING, ACE);

        firstCardHBox = new HBox(new Label("Card Number:"), firstCardNumberChoiceBox);
        firstCardHBox.setAlignment(Pos.CENTER);
        firstCardHBox.setSpacing(10);

        submitButton = new Button("Submit");
        submitButton.setOnAction(event ->
        {
            Poker pokerGame = new Poker();

            Poker.Number firstCardNumber = firstCardNumberChoiceBox.getValue();

            if (firstCardNumber == null)
            {
                throw new NullPointerException("firstCardNumber can't be null");
            }

            if (firstCardNumber == ACE)
            {
                firstCard = new Poker.Card(firstCardNumber, SPADE);
            }

            else if(firstCardNumber == KING)
            {
                firstCard = new Poker.Card(firstCardNumber, HEART);
            }

            else if(firstCardNumber == FIVE)
            {
                firstCard = new Poker.Card(firstCardNumber, DIAMOND);
            }

            else
            {
                firstCard = new Poker.Card(firstCardNumber, CLUB);
            }
}

        VBox vBox = new VBox(firstCardHBox, submitButton);
        vBox.setSpacing(10);
        vBox.setAlignment(Pos.CENTER);
        vBox.setPadding(new Insets(10));
        return new Scene(vBox);

我有一个choicebox,它被设置为包含我创建的枚举中的四个项。
firstCardNumberChoiceBox = new ChoiceBox<>(); firstCardNumberChoiceBox.getItems().addAll(TWO, FIVE, KING, ACE); 一开始,我将默认值设置为2,这样它就不是空的。 choiceBox.setValue(TWO); 然后我创建了一个submit按钮,可以检查选项框的值。

submitButton.setOnAction(event ->
        {
            Poker pokerGame = new Poker();

            Poker.Number firstCardNumber = firstCardNumberChoiceBox.getValue();

            if (firstCardNumber == null)
            {
                throw new NullPointerException("firstCardNumber can't be null");
            }

            if (firstCardNumber == ACE)
            {
                firstCard = new Poker.Card(firstCardNumber, SPADE);
            }
            else if(firstCardNumber == KING)
            {
                firstCard = new Poker.Card(firstCardNumber, HEART);
            }

            else if(firstCardNumber == FIVE)
            {
                firstCard = new Poker.Card(firstCardNumber, DIAMOND);
            }

            else
            {
                firstCard = new Poker.Card(firstCardNumber, CLUB);
            }
        }

我第一次工作很好,但每次之后,它总是将firstcardnumber设置为2,即使我单击不同的值。我试着放弃设置默认值,结果发现不管我点击其他什么值,该值总是设置为null。

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: firstCardNumber can't be null
    at KTANE.Bot/sample.Scenes.PokerScene.lambda$getPokerScene$1(PokerScene.java:68)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
    at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
    at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
    at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
    at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
    at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
    at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
    at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
    at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
    at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
    at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
    at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
    at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
    at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
    at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
    at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
    at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
    at java.base/java.lang.Thread.run(Thread.java:834)

我以前和ChoiceBox一起工作过很多次,但这从来没有发生在我身上。我可能做错了什么,我能做些什么来修复它?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题