How do I make a pane's contents stay on the screen in JavaFX?

时间:2018-01-23 19:22:10

标签: java javafx

I'm currently making a game for my class - Jeopardy. I'm using JavaFX and have run into a problem when switching the content for my scene. The following is my code. The problem is that the screen switches from the secondToGame()'s contents to the thirdToGame()'s contents and THEN BACK to secondToGame(). It shouldn't go back to secondToGame()'s contents. Why is it doing this?

private void secondToGame() {
    //Created four Text objects successfully named "computerScienceText", "physicsText", "calculusText", and "randomText"

    Text[] options = {computerScienceText, physicsText, calculusText, randomText}; //Creates an array of options
    for (Text e : options) { //Runs through the array and adds mouse capabilities
        e.setOnMousePressed(new EventHandler<>() {
            /**
             * We're going to make it so that when a text is clicked, data will be used for a game of Jeopardy,
             * and create another screen containing the game.
             * @param me me will take notice of when a text is clicked.
             */
            @Override
            public void handle(MouseEvent me) {
                File file;
                if (computerScienceText.equals(me.getSource())) {
                    //Choose file pertaining to computer science. Only this one has data so far.
                    file = new File("computerScienceDataForJeopardy.csv");
                    thirdToGame(file);
                } else if (physicsText.equals(me.getSource())) {
                    //Choose file pertaining to physics.
                    file = new File("physicsDataForJeopardy.csv");
                    thirdToGame(file);
                } //Repeated for the other Text objects
            }
        });
    }

    HBox categories = new HBox();
    categories.setPadding(new Insets(20, 20, 20, 20));
    categories.setSpacing(30);
    categories.getChildren().addAll(computerScienceText, physicsText, calculusText, randomText);
    categories.setAlignment(Pos.CENTER);

    BorderPane secondPane = new BorderPane();
    secondPane.setCenter(categories);
    //Added the style to secondPane successfully

    theScene.setRoot(secondPane); //Keeps fullscreen mode on and changes the screen.
}

private void thirdToGame(File file) {
    //Successfully created Text objects named "firstCategory", "secondCategory", ..., "fifthCategory"

    HBox gameCategories = new HBox();
    gameCategories.setPadding(new Insets(20, 20, 20, 20));
    gameCategories.setSpacing(30);
    gameCategories.getChildren().addAll(firstCategory, secondCategory, thirdCategory, fourthCategory, fifthCategory);
    gameCategories.setAlignment(Pos.TOP_CENTER);

    GridPane gameChoicesInCategories = new GridPane();
    gameChoicesInCategories.setPadding(new Insets(10, 10, 10, 10));
    gameChoicesInCategories.setMinSize(200, 200); //Set the minimum size of the classPane.
    gameChoicesInCategories.setVgap(5); //Set a vertical gap between components of 5 pixels.
    gameChoicesInCategories.setHgap(5); //Set a horizontal gap between components of 5 pixels.
    //Added the style to gameChoicesInCategories successfully
    gameChoicesInCategories.setAlignment(Pos.CENTER);

    BorderPane thirdPane = new BorderPane();
    thirdPane.setTop(gameCategories);
    thirdPane.setCenter(gameChoicesInCategories);
    //Added style to thirdPane successfully

    theScene.setRoot(thirdPane);
}

Thank you very much. Bonus points for an easy explanation.

Here is the MCV Example:

import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.util.Duration;

import java.io.File;
public class MCV extends Application {

private Scene theScene;
private Stage theStage;

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

@Override
public void start(Stage primaryStage) {
    theStage = primaryStage;
    firstToGame();
    primaryStage.show();
    primaryStage.setFullScreen(true);
}

private void firstToGame() {
    Text gameText = new Text("Jeopardy");

    Text beginGameText = new Text("\n\n\nBegin playing");
    beginGameText.setOnMousePressed(new EventHandler<>() {
        @Override
        public void handle(MouseEvent me) {
            beginGameText.setText("\n\n\nLoading...");
            Timeline timeline = new Timeline(new KeyFrame(Duration.millis(3000), ae -> secondToGame()));
            timeline.setCycleCount(Animation.INDEFINITE);
            timeline.play(); //After 3 seconds, the screen will change to the classPane.
        }
    });
    StackPane greetingPane = new StackPane();
    greetingPane.getChildren().addAll(beginGameText, gameText);
    greetingPane.setAlignment(beginGameText, Pos.CENTER);
    greetingPane.setAlignment(gameText, Pos.CENTER);

    BorderPane firstPane = new BorderPane();
    firstPane.setCenter(greetingPane);
    theScene = new Scene(firstPane, 1200, 600);
    theStage.setScene(theScene);
}

private void secondToGame() {
    Text computerScienceText = new Text("Computer Science");
    Text physicsText = new Text("Physics");
    Text calculusText = new Text("Calculus");
    Text randomText = new Text("Random");

    Text[] options = {computerScienceText, physicsText, calculusText, randomText};
    for (Text e : options) {
        e.setOnMousePressed(new EventHandler<>() {
            @Override
            public void handle(MouseEvent me) {
                File file;
                if (computerScienceText.equals(me.getSource())) {
                    file = new File("computerScienceDataForJeopardy.csv");
                    thirdToGame(file);
                } else if (physicsText.equals(me.getSource())) {
                    file = new File("physicsDataForJeopardy.csv");
                    thirdToGame(file);
                } else if (calculusText.equals(me.getSource())) {
                    file = new File("calculusDataForJeopardy.csv");
                    thirdToGame(file);
                } else if (randomText.equals(me.getSource())) {
                    file = new File("randomDataForJeopardy.csv");
                    thirdToGame(file);
                }
            }
        });
    }

    HBox categories = new HBox();
    categories.setPadding(new Insets(20, 20, 20, 20));
    categories.setSpacing(30);
    categories.getChildren().addAll(computerScienceText, physicsText, calculusText, randomText);
    categories.setAlignment(Pos.CENTER);

    BorderPane secondPane = new BorderPane();
    secondPane.setCenter(categories);
    theScene.setRoot(secondPane); //Keeps fullscreen mode on and changes the screen.
}

private void thirdToGame(File file) {
    Text firstCategory = new Text("First Category");
    Text secondCategory = new Text("Second Category");
    Text thirdCategory = new Text("Third Category");
    Text fourthCategory = new Text("Fourth Category");
    Text fifthCategory = new Text("FIfth Category");

    HBox gameCategories = new HBox();
    gameCategories.setPadding(new Insets(20, 20, 20, 20));
    gameCategories.setSpacing(30);
    gameCategories.getChildren().addAll(firstCategory, secondCategory, thirdCategory, fourthCategory, fifthCategory);
    gameCategories.setAlignment(Pos.TOP_CENTER);

    GridPane gameChoicesInCategories = new GridPane();
    gameChoicesInCategories.setPadding(new Insets(10, 10, 10, 10));
    gameChoicesInCategories.setMinSize(200, 200); //Set the minimum size of the classPane.
    gameChoicesInCategories.setVgap(5); //Set a vertical gap between components of 5 pixels.
    gameChoicesInCategories.setHgap(5); //Set a horizontal gap between components of 5 pixels.

    gameChoicesInCategories.setAlignment(Pos.CENTER);

    BorderPane thirdPane = new BorderPane();
    thirdPane.setTop(gameCategories);
    thirdPane.setCenter(gameChoicesInCategories);

    theScene.setRoot(thirdPane);
}

1 个答案:

答案 0 :(得分:0)

firstToGame(...)的处理程序中,行

 timeline.setCycleCount(Animation.INDEFINITE) ;

导致时间轴无限重复。所以三秒钟,执行secondToGame()方法,导致菜单屏幕每三秒重新显示一次。

只需删除该行:默认cycleCount1,这就是您想要的。

相关问题