递归移动JavaFX文本标签的共同点

时间:2019-03-02 02:31:07

标签: recursion javafx

我的程序获取一个用户值,并在通过GUI返回消息之前,将该值通过方法传递。我想知道如何能够在用户再次调用该方法时沿Y轴向下递归移动此消息(称为displayResult)值,以便为新消息腾出空间。

public class GuessingGameController extends Tab {

    Scene sceneOne;
    Scene sceneTwo;
    int choice;
    int generatedNumber;
    //private GuessingGame resetGuessingGame;

    public int generateRandomNumber() {

        Random randomNumber = new Random();
        int number = randomNumber.nextInt(50) + 1;
        return number;
    }

    GuessingGameController(Stage primaryStage) {

        GuessingGame guess = new GuessingGame();
        generatedNumber = generateRandomNumber();

        Label labelOne = new Label("WELCOME TO THE GUESSING GAME");
        setText("GuessingGame");
        Pane paneLayoutOne = new StackPane();
        Pane paneLayoutTwo = new StackPane();
        Button playButton = new Button("Click to Play!");
        Button quitButton = new Button("Quit");

        paneLayoutOne.getChildren().addAll(labelOne, playButton, quitButton);

        labelOne.setTranslateX(0);
        labelOne.setTranslateY(-100);

        playButton.setTranslateX(0);
        playButton.setTranslateY(0);

        quitButton.setTranslateX(0);
        quitButton.setTranslateY(50);

        setContent(paneLayoutOne);

        sceneOne = new Scene(paneLayoutOne, 400, 400);

        quitButton.setOnAction(e -> System.exit(0));

        playButton.setOnAction(e -> primaryStage.setScene(sceneTwo));

        Label userMessage = new Label("Please enter your guess here");
        TextField userInput = new TextField();
        Button guessButton = new Button("Guess");
        Button resetButton = new Button("Reset Game");
        Button backButton = new Button("Back");

        userMessage.setTranslateX(0);
        userMessage.setTranslateY(-160);

        userInput.setTranslateX(0);
        userInput.setTranslateY(-120);

        guessButton.setTranslateX(0);
        guessButton.setTranslateY(-80);

        resetButton.setTranslateX(0);
        resetButton.setTranslateY(-40);

        backButton.setTranslateX(0);
        backButton.setTranslateY(0);

        guessButton.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {

                int choice = Integer.parseInt(userInput.getText());
                guess.startGuessingGame(choice, generatedNumber);

                String endResult;

                endResult = guess.startGuessingGame(choice, generatedNumber);

                Label displayResult = new Label(endResult);

                displayResult.setTranslateX(0);
                displayResult.setTranslateY(150);
                paneLayoutTwo.getChildren().add(0, displayResult);
            }
        });

        //resetButton.setOnAction(e -> resetGuessingGame.resetForm(this));
        backButton.setOnAction(e -> primaryStage.setScene(sceneOne));

        paneLayoutTwo.getChildren().addAll(userMessage, userInput, guessButton, resetButton, backButton);

        sceneTwo = new Scene(paneLayoutTwo, 400, 400);
    }
}

0 个答案:

没有答案