简体   繁体   English

如何在JavaFX中将窗格的内容保留在屏幕上?

[英]How do I make a pane's contents stay on the screen in 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. 我正在使用JavaFX,在切换场景内容时遇到问题。 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(). 问题是屏幕从secondToGame()的内容切换到thirdToGame()的内容,然后从THEN BACK切换到secondToGame()。 It shouldn't go back to secondToGame()'s contents. 它不应该回到secondToGame()的内容。 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: 这是MCV示例:

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);
}

In the handler in firstToGame(...) , the line firstToGame(...)的处理程序中,该行

 timeline.setCycleCount(Animation.INDEFINITE) ;

causes the timeline to repeat indefinitely. 导致时间轴无限期重复。 So every three seconds, your secondToGame() method is executed, causing the menu screen to be redisplayed every three seconds. 因此, 三秒钟执行一次 secondToGame()方法,从而使菜单屏幕每三秒钟重新显示一次。

Just remove that line: the default cycleCount is 1 , which is what you want. 只需删除该行:默认的cycleCount1 ,这就是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM