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

时间:2017-12-17 19:33:08

标签: java javafx

我是JavaFX的新手,对Java有一些了解。 我的目标是开发桌面广泛的游戏。 它应该询问玩家某些选项并确定游戏将如何进行(如投票,选择房间等) 它使用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();


}

}

1 个答案:

答案 0 :(得分:1)

  

if (playersChoice.isOk()){//it stop working here

它并没有停止在那里工作。它没有开始工作。在您单击startGame之前,它默认为false,如果选择正常,则操作不会再次检查。

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

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

您的下一个问题是mainWindow.setScene(welcomeScene);之前welcomeScene = new Scene(welcomePlayerslayout, WIDTH, HEIGHT);