Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javafx中单击按钮后如何保存/存储变量_Java_Javafx - Fatal编程技术网

在javafx中单击按钮后如何保存/存储变量

在javafx中单击按钮后如何保存/存储变量,java,javafx,Java,Javafx,我对JavaFX非常陌生,对javatho有一些了解。 我的目标是开发一款桌面游戏。 它应该向玩家询问某些选项,并确定游戏将如何进行(如投票、选择室等) 它在使用Scanner的Java中工作得很好,但在JavaFX中却不行; 每当我单击按钮时,它只影响块中的变量。 我确实需要这些选择的选项来支持游戏逻辑。 请帮忙 更新-我将if语句移动到动作块中,但它仍然没有将场景设置为欢迎 public class Main extends Application{ Button startGame,how

我对JavaFX非常陌生,对javatho有一些了解。 我的目标是开发一款桌面游戏。 它应该向玩家询问某些选项,并确定游戏将如何进行(如投票、选择室等) 它在使用Scanner的Java中工作得很好,但在JavaFX中却不行; 每当我单击按钮时,它只影响块中的变量。 我确实需要这些选择的选项来支持游戏逻辑。 请帮忙

更新-我将if语句移动到动作块中,但它仍然没有将场景设置为欢迎

public class Main extends Application{
Button startGame,howTOPlay, viewRoomButton, viewItemListButton;
List<Scene> playersenes = new ArrayList<>();
Scene welcomeScene, playerRedScene, playerYellowScene, playerBlueScene, playerGreenScene, playerBrownScene, playerBlackScene;
Stage mainWindow;
static int numberOfPlayers;
final static int WIDTH =800;
final static int HEIGHT=600;

;

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

@Override
public void start(Stage primaryStage) throws Exception {


    //mainWidow setup
    mainWindow = primaryStage;
    mainWindow.setTitle("Mall of Horror");

    //startgameScene
    List<Integer> numberOfPlayersOptions = new ArrayList<>();
    numberOfPlayersOptions.add(4);
    numberOfPlayersOptions.add(5);
    numberOfPlayersOptions.add(6);

    GameBroad gameBroad=new GameBroad(1);
    PlayersChoice playersChoice = new PlayersChoice();

    startGame = new Button();
    startGame.setText("Start Game");
    startGame.setOnAction(event -> {
        //numberwindow.display is a new stage with a method return a static int
        //gamebroad is class with models, which require the number of players to support its logic.
        gameBroad.setPlayersNumber(NumberWindow.display(numberOfPlayersOptions, "please select how many players"));

        //playerChoise is a class, ok is default fault boolean. set Ok is method to set the ok boolean to true;
        playersChoice.setOk();
        mainWindow.setScene(welcomeScene);


        if (playersChoice.isOk()){//it stop working here
            //welcomeScene;
            Label welcome = new Label();
            welcome.textProperty().setValue("Welcome  players " + gameBroad.getPlayersNumber() );

            Button ok1 = new Button("OK");
            ok1.setOnAction(event1 -> {
                mainWindow.setScene(playerRedScene);
            });
            VBox welcomePlayerslayout = new VBox();
            welcomePlayerslayout.getChildren().addAll(welcome,ok1);
            welcomeScene = new Scene(welcomePlayerslayout, WIDTH, HEIGHT);

            //playerRedScene;

            viewRoomButton = new Button();
            viewRoomButton.setText("Rooms");

            viewItemListButton = new Button();
            viewItemListButton.setText("Items");

            VBox playerRedLayout = new VBox();
            playerRedLayout.getChildren().addAll(viewRoomButton,viewItemListButton);
            playerRedScene = new Scene(playerRedLayout, 800,600);

        }
        System.out.println(playersChoice.isOk()); //it shows true here;
    });

    howTOPlay = new Button();
    howTOPlay.setText("How to Play");

    VBox firstlayout = new VBox();
    firstlayout.getChildren().addAll(startGame, howTOPlay);
    Scene firstscene = new Scene(firstlayout, WIDTH, HEIGHT);
    primaryStage.setScene(firstscene);
    mainWindow.show();


}
public类主扩展应用程序{
按钮StartName、howTOPlay、viewRoomButton、viewItemListButton;
List playersenes=new ArrayList();
场景welcomeScene、playerRedScene、PlayerEllowscene、playerBlueScene、PlayerGreensecene、playerBrownScene、playerBlackScene;
舞台主窗口;
静态整数玩家;
最终静态整数宽度=800;
最终静态内部高度=600;
;
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
public void start(Stage primaryStage)引发异常{
//主窗口设置
主窗口=初级阶段;
主窗口。设置标题(“恐怖广场”);
//新发中新世
List NumberOfPlayerOptions=new ArrayList();
数字播放器选项。添加(4);
数字播放器选项。添加(5);
数字播放器选项。添加(6);
GameBroad GameBroad=新GameBroad(1);
PlayersChoice PlayersChoice=新PlayersChoice();
startName=新按钮();
startGame.setText(“开始游戏”);
StartName.setOnAction(事件->{
//numberwindow.display是一个新阶段,其方法返回静态int
//gamebroad是一个模型类,需要玩家数量来支持其逻辑。
gameBroad.setPlayersNumber(NumberWindow.display(numberOfPlayersOptions,“请选择有多少玩家”);
//PlayerChoice是一个类,ok是默认的错误布尔值。set ok是将ok布尔值设置为true的方法;
playerChoice.setOk();
主窗口。设置场景(Welcome场景);
如果(playerChoice.isOk()){//它将在此处停止工作
//欢迎来到现场;
标签欢迎=新标签();
welcome.textProperty().setValue(“欢迎玩家”+gameBroad.getPlayerNumber());
按钮ok1=新按钮(“OK”);
ok1.设置操作(事件1->{
主窗口.setScene(playerRedScene);
});
VBox WelcomePlayerLayout=新的VBox();
WelcomePlayerLayout.getChildren().addAll(欢迎,ok1);
welcomeScene=新场景(WelcomePlayers布局、宽度、高度);
//playerRedScene;
viewRoomButton=新建按钮();
viewRoomButton.setText(“房间”);
viewItemListButton=新建按钮();
viewItemListButton.setText(“项目”);
VBox playerRedLayout=new VBox();
playerRedLayout.getChildren().addAll(viewRoomButton、viewItemListButton);
playerRedScene=新场景(playerRedLayout,800600);
}
System.out.println(playersChoice.isOk());//这里显示为true;
});
howTOPlay=新按钮();
howTOPlay.setText(“如何玩”);
VBox firstlayout=新的VBox();
firstlayout.getChildren().addAll(startGame,howTOPlay);
场景第一场景=新场景(第一布局、宽度、高度);
初级阶段。设置场景(第一场景);
mainWindow.show();
}
}

if(playersChoice.isOk()){//它在这里停止工作

它没有停止工作。它没有开始工作。它默认为false,直到您单击
startGame
,并且该操作不会再次检查选择是否正确

因此,您需要在开始按钮操作中设置欢迎操作,该操作仅在单击按钮后运行

在当前代码中,if语句在按钮单击之前运行,尽管代码已经写在操作下面。换句话说,GUI编程没有明确地从上到下运行



下一个问题是在
welcomsecene=新场景(welcomePlayerslayout,WIDTH,HEIGHT)之前有
main window.setScene(welcomsecene);

您需要将if语句移动到“开始”按钮单击操作侦听器感谢您的响应。按照建议移动,但它仍然不会进入欢迎场景。您所说的“停止工作”到底是什么意思?您是否收到异常?或者
if
中的条件是否只返回false(因此,
如果
块未执行)?如果出现异常,则发布完整的堆栈跟踪。如果
playersChoice.isOk()
未给出预期结果,则发布
playersChoice
类。“这是由于多线程造成的”。嗯,不完全是。所有发布的代码都在单个线程上运行。真正的原因是属于提供给
setOnAction
的对象的方法不会立即被调用;它们会在操作发生时被调用。控制流类似于:call
start()
,然后启动一个循环,检查操作并调用操作处理程序。但所有这些都发生在一个线程上。谢谢。我想这就是我被卡住的原因。我将研究如何安排线程。@Dexter.Z正如我在评论中所指出的,这与线程无关。正如答案中所说,只需将相关逻辑移到操作句柄即可呃。嗨,詹姆斯,谢谢你的回复。我将if语句移动到动作处理程序,但仍然不会更改为欢迎场景:(@Dexter.Z现在是否有理由检查if语句?您
setOk
因此
isOk
始终为真…如果没有显示任何内容,则说明其他错误,例如在定义
welcomese
之前调用
main window.setScene(welcomeScene);