JavaFx状态更改侦听器

时间:2014-01-02 22:48:19

标签: events menu javafx handler scene

我正在开发一个我希望实现菜单的应用程序。我有一个GameState和一个MainMenu课程。两者都延伸Group。我无法弄清楚如何将更改侦听器写入Main.state,因此当它从.MENU更改为.GAME时,场景将会切换。

这是MainMenu类的一部分:

public class MainMenu extends Group {
private final Image background;
private final Rectangle bgRect;
private final int buttonNo = 3;
private MenuButton[] buttons;
private final double xStart = -200;
private final double yStart = 100;
private Group root;
private Scene scene;


public MainMenu () {
    background = new Image(getClass().getResource("mainmenuBg.png").toString());        
    bgRect = new Rectangle(660,660);
    bgRect.setFill(new ImagePattern(background));

    root = new Group();
    scene = new Scene(root, 650, 650);
    scene.setCamera(new PerspectiveCamera());

    root.getChildren().add(bgRect);

    initButtons(root);

    //Start game
    buttons[0].setOnMouseClicked(new EventHandler<Event>() {

        @Override
        public void handle(Event t) {
            Main.state = STATE.GAME;
        }
    });

    //Options
    buttons[1].setOnMouseClicked(new EventHandler<Event>() {

        @Override
        public void handle(Event t) {
            //options menu will come here
        }
    });

    //Exit
    buttons[2].setOnMouseClicked(new EventHandler<Event>() {

        @Override
        public void handle(Event t) {
            Platform.exit();
        }
    });
}
 //...
}

主要班级:

public class Main extends Application {
public int difficulty = 1;
GameState gameState;
MainMenu mainMenu;

public enum STATE {
    MENU,
    GAME
}

public static STATE state = STATE.MENU;

@Override
public void start(Stage stage) {  
    stage.resizableProperty().setValue(false);
    stage.setTitle("Main");
    Scene scene = new Scene(new StackPane(), 650, 650);

    stage.setScene(scene);
    stage.show();

    if(Main.state == STATE.MENU)
        enterMainMenu(stage);
    if(Main.state == STATE.GAME)
        enterGameState(stage);


}
//...
}

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

我成功地找到了一个好的解决方案。 我已从这些类中删除了scene字段,并在构造函数中添加了super方法,而不是将元素添加到类(this.getChildren().addAll(..))。

最后,这是我的主控制器:

public class Main extends Application {
public int difficulty = 1;
public GameState gameState = new GameState(difficulty);
public MainMenu mainMenu = new MainMenu();;
StackPane stackPane = new StackPane();

@Override
public void start(final Stage stage) {  
    stage.resizableProperty().setValue(false);
    stage.setTitle("Main");
    Scene scene = new Scene(stackPane, 650, 650);    
    scene.setCamera(new PerspectiveCamera());

    stage.setScene(scene);
    stage.show();

    stackPane.getChildren().add(mainMenu);

    mainMenu.getStartButton().setOnAction(new EventHandler<ActionEvent>() {

        @Override
        public void handle(ActionEvent t) {
            changeScene(gameState);
            try {
                gameState.startGame();
            } catch (InterruptedException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    });

}

public void changeScene(Parent newPage) {
    stackPane.getChildren().add(newPage);

  EventHandler<ActionEvent> finished = new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
      stackPane.getChildren().remove(0);
    }
  };

  final Timeline switchPage = new Timeline(
    new KeyFrame(Duration.seconds(0), new KeyValue(stackPane.getChildren().get(1).opacityProperty(), 0.0), new KeyValue(stackPane.getChildren().get(0).opacityProperty(), 1.0)),
    new KeyFrame(Duration.seconds(3), finished, new KeyValue(stackPane.getChildren().get(1).opacityProperty(), 1.0), new KeyValue(stackPane.getChildren().get(0).opacityProperty(), 0.0))
  );

  switchPage.play();
}
}