簡體   English   中英

如何在JavaFX中將窗格的內容保留在屏幕上?

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

我目前正在為我的課程制作游戲-危險游戲。 我正在使用JavaFX,在切換場景內容時遇到問題。 以下是我的代碼。 問題是屏幕從secondToGame()的內容切換到thirdToGame()的內容,然后從THEN BACK切換到secondToGame()。 它不應該回到secondToGame()的內容。 為什么這樣做呢?

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

非常感謝你。 獎勵積分,方便說明。

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

firstToGame(...)的處理程序中,該行

 timeline.setCycleCount(Animation.INDEFINITE) ;

導致時間軸無限期重復。 因此, 三秒鍾執行一次 secondToGame()方法,從而使菜單屏幕每三秒鍾重新顯示一次。

只需刪除該行:默認的cycleCount1 ,這就是您想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM