为什么我的听众没有解雇这个活动?

时间:2018-06-05 09:54:07

标签: java model-view-controller listener

短而甜蜜。我有一个MenuItemListener,它使用控制器类执行MenuItem的setOnAction,除了它没有触发/工作。我在控制器类的构造函数中使用lambda表达式来触发视图类中方法中的处理程序,但它不起作用。我无法解决这个问题。谢谢!

查看:

public class View extends Application {

    private Stage stage; // A main window
    private Scene scene; // Content in main window
    private int width = 640, height = 480;
    private BorderPane root;
    private MenuBar menuBar;
    private Menu menuFile, menuEdit, menuView, menuHelp, menuAbout;
    public MenuItem Open, Save, Close, Exit, Copy, Paste, Maximise, Minimise, Docs, Version;

    @Override
    public void start(Stage primaryStage) throws Exception {
        stage = primaryStage;
        init();
        root = new BorderPane();
        Label label = new Label("Main Screen");
        root.setCenter(label);
        root.setTop(menuBar);           
        scene = new Scene(root, width, height);     
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        stage.setScene(scene);
        stage.setTitle("Title");
        stage.show();
    }

    public static void main (String[] args) {
        Application.launch(args);
    }

    public void init() {
        menuBar = new MenuBar();

        menuFile = new Menu("File");
        menuEdit = new Menu("Edit");
        menuView = new Menu("View");
        menuHelp = new Menu("Help");
        menuAbout = new Menu("About");

        Open = new MenuItem("Open");
        Save = new MenuItem("Save");
        Close = new MenuItem("Close");
        Exit = new MenuItem("Exit");
        Copy = new MenuItem("Copy");
        Paste = new MenuItem("Paste");
        Maximise = new MenuItem("Maximise");
        Minimise = new MenuItem("Minimise");
        Docs = new MenuItem("Docs");
        Version = new MenuItem("Version");

        menuFile.getItems().addAll(Open, Save, Close, Exit);
        menuEdit.getItems().addAll(Copy, Paste);
        menuView.getItems().addAll(Maximise, Minimise);
        menuHelp.getItems().addAll(Docs);
        menuAbout.getItems().addAll(Version);

        menuBar.getMenus().addAll(menuFile, menuEdit, menuView, menuHelp, menuAbout);
    }

    public void MenuItemListener(MenuItem menuItem, EventHandler<ActionEvent> handler) {
        menuItem.setOnAction(handler);
    }

    public Scene getScene() {
        return scene;
    }

    public void setScene(Scene scene) {
        this.scene = scene;
    }

    public Stage getStage() {
        return stage;
    }

    public void setStage(Stage stage) {
        this.stage = stage;
    }

}

控制器:

public class Controller {

    private View view;
    private Model model;

    Controller(View v, Model m) {
        this.view = v;
        this.model = m;

        this.view.MenuItemListener(this.view.Exit, handler -> {
            System.exit(0);
        });

        this.view.MenuItemListener(this.view.Maximise, handler -> {
            view.getStage().fullScreenProperty();
        });

        this.view.MenuItemListener(this.view.Minimise, handler -> {
            view.getStage().setIconified(true);
        });

    }   
}

0 个答案:

没有答案