Возвращаясь к предыдущей сцене в JavaFX - PullRequest
0 голосов
/ 28 октября 2019

Я пытаюсь добавить кнопку в своем назначении Pokergame, чтобы вернуться со сцены, где вы можете играть (sce), в сцену, содержащую главное меню (scene1).

Насколько я знаю, метод переключения сцен должен выглядеть примерно так:

private void setBack() {
  view.getBackButton().setOnAction(e -> view.stage.setScene(scene1));

    }

К сожалению, затмение показывает мне некоторые ошибки, которые говорят, что сцена не может быть решена, а сцена1 не может бытьразрешается в переменную.

Я добавлю сюда дополнительный код для лучшего понимания проблемы:

Класс контроллера

public class PokerGameController {
private PokerGameModel model;
private PokerGameView view;

public PokerGameController(PokerGameModel model, PokerGameView view) {
    this.model = model;
    this.view = view;

    view.getShuffleButton().setOnAction( e -> shuffle() );
    view.getDealButton().setOnAction( e -> deal() );
    view.getBackButton().setOnAction(e -> setBack() );
    view.getAddButton().setOnAction(e -> AddnewPlayer());
}

Класс просмотра

public class PokerGameView {
private HBox players;
private ControlArea controls;

private PokerGameModel model;

public PokerGameView(Stage stage, PokerGameModel model) {
    this.model = model;

    // Creation of the Setup scene

    Label lbs = new Label("Welcome to the SE Poker Miniproject!");
    Label lbs2 = new Label("Four players max!");
    Label lbs3 = new Label("How many players are you?");

    Button chng = new Button ("Start!");

    BorderPane boot = new BorderPane();
    boot.setTop(lbs);
    boot.setCenter(chng);

    Scene scene1 = new Scene(boot, 400, 400);
    stage.setTitle("Poker");
    stage.setScene(scene1);
    stage.show();

    // Create the control area
    controls = new ControlArea();
    controls.linkDeck(model.getDeck()); // link DeckLabel to DeckOfCards in the logic

    // Put players and controls into a BorderPane
    BorderPane root = new BorderPane();
    root.setCenter(players);
    root.setBottom(controls);

    // Disallow resizing - which is difficult to get right with images
    stage.setResizable(false);

    // Create the scene using our layout; then display it
    Scene sce = new Scene(root);
    sce.getStylesheets().add(
            getClass().getResource("poker.css").toExternalForm());
    stage.setTitle("Poker Miniproject");
    stage.setScene(scene1);
    stage.show();   

    // Switching to the main Scene
    chng.setOnAction(e-> stage.setScene(sce));
}

public PlayerPane getPlayerPane(int i) {
    return (PlayerPane) players.getChildren().get(i);
}

public Button getShuffleButton() {
    return controls.btnShuffle;
}

public Button getDealButton() {
    return controls.btnDeal;
}

public Button getBackButton() {
    return controls.btnBack;
}

public Button getAddButton() {
return controls.btnAdd;
}

}

И класс ControlArea:

public class ControlArea extends HBox{
private DeckLabel lblDeck = new DeckLabel();
private Region spacer = new Region(); // Empty spacer
Button btnShuffle = new Button("Shuffle");
Button btnDeal = new Button("Deal");
Button btnBack = new Button("Back to Menu");
Button btnAdd = new Button("New Player");

public ControlArea() {
    super(); // Always call super-constructor first !!

    this.getChildren().addAll(lblDeck, spacer, btnBack, btnAdd, btnShuffle, btnDeal);

    HBox.setHgrow(spacer, Priority.ALWAYS); // Use region to absorb resizing
    this.setId("controlArea"); // Unique ID in the CSS
}

Если вам нужна дополнительная информация, пожалуйста, дайте мне знать.

...