返回JavaFX中的上一个场景

时间:2019-10-28 16:26:06

标签: java javafx model-view-controller scene poker

我正在尝试在我的扑克游戏分配中添加一个按钮,以从您可以播放(场景)的场景返回到包含主菜单(场景1)的场景。

据我所知,切换场景的方法必须看起来像这样:

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

    }

不幸的是,eclipse向我显示了一些错误,这些错误表明无法解析舞台并且无法将scene1解析为变量。

我将在此处添加一些其他代码,以更好地理解问题:

Controller类

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

View类

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
}

如果您需要更多信息,请告诉我。

0 个答案:

没有答案
相关问题