如何在MVC | JavaFX中将事件处理程序附加到控制器

如何在MVC | JavaFX中将事件处理程序附加到控制器,javafx,model-view-controller,Javafx,Model View Controller,我很难理解如何将mvc模式应用到JavaFX 以下是我关于以下代码的问题,因为我需要遵循代码中给出的模式: a) 如何将ViewA中存在的按钮的事件处理程序附加到ControllerA中的代码(特别是,attachEventHandlers()方法)。例如,我希望我的按钮使用控制器中的getModelItems()方法的结果填充ViewA中的组合框 请注意,方法getModelItems()是私有的 b) 我的视图中会有多个按钮和事件处理程序。如何将它们中的每一个唯一地绑定到控制器 c) 我想在

我很难理解如何将mvc模式应用到JavaFX

以下是我关于以下代码的问题,因为我需要遵循代码中给出的模式:

a) 如何将ViewA中存在的按钮的事件处理程序附加到ControllerA中的代码(特别是,
attachEventHandlers()
方法)。例如,我希望我的按钮使用控制器中的
getModelItems()
方法的结果填充ViewA中的组合框

请注意,方法
getModelItems()
是私有的

b) 我的视图中会有多个按钮和事件处理程序。如何将它们中的每一个唯一地绑定到控制器

c) 我想在控制器中的模型上调用
setName(String name)
,我想传递的参数是viewA中组合框上的选定值。我怎样才能做到这一点

非常感谢你的帮助

下面是说明中提到的代码

控制器:

import model.ModelA;
import view.ViewA;
public class ControllerA {

    private ViewA view;

    private ModelA model;

    public ControllerA(ViewA view, ModelA model) {
        //initialise model and view fields
        this.model = model;
        this.view = view;

        //populate combobox in ViewB, e.g. if viewB represented your ViewB you could invoke the line below
        //viewB.populateComboBoxWithCourses(setupAndRetrieveCourses());

        this.attachEventHandlers(); 

    }

    private void attachEventHandlers() {
    }


    private String[] getModelItems() {

        String[] it = new String[2];
        it[0] = "0";
        it[1] = "1";

        return it;
    }

}
型号:

public class ModelA {

    private String name;


    public Name() {
        name = "";
    }



    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Name = " + name;
    }

}
视图:

加载器:

public class ApplicationLoader extends Application {

    private ViewA view;

    @Override
    public void init() {
        //create model and view and pass their references to the controller
        ModelA model = new ModelA();
        view = new ViewA();
        new ControllerA(view, model);   
    }

    @Override
    public void start(Stage stage) throws Exception {
        //whilst you can set a min width and height (example shown below) for the stage window,
        //you should not set a max width or height and the application should
        //be able to be maximised to fill the screen and ideally behave sensibly when resized
        stage.setMinWidth(530); 
        stage.setMinHeight(500);


        stage.setTitle("Final Year Module Chooser Tool");
        stage.setScene(new Scene(view));
        stage.show();
    }

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

}

将代理添加到视图A以允许访问:

public class ViewA extends BorderPane {
    ComboBox comboBox;
    Button button1;

    public ViewA(){
        comboBox = new ComboBox();
        button1 = new Button("Populate");
        setTop(button1);
        setBottom(comboBox);
    }

    // Add delegates for all functionality you want to make available through ViewA
    public ObservableList<String> getItems() { return comboBox.getItems(); }
    public void setOnButton1Action(...) { ... }
    public void setOnButton2Action(...) { ... }
    ...
}
公共类视图扩展边框窗格{
组合框组合框;
按钮1;
公众视野a(){
comboBox=新的comboBox();
button1=新按钮(“填充”);
setTop(按钮1);
setBottom(组合框);
}
//为要通过ViewA提供的所有功能添加代理
公共ObservableList getItems(){return comboBox.getItems();}
public void setOnButton1Action(…){…}
公共无效setOnButton2Action(…){…}
...
}

根据您希望通过ViewA管理的内容,您可以选择任意宽或窄。

您可以将代理添加到ViewA以允许访问:

public class ViewA extends BorderPane {
    ComboBox comboBox;
    Button button1;

    public ViewA(){
        comboBox = new ComboBox();
        button1 = new Button("Populate");
        setTop(button1);
        setBottom(comboBox);
    }

    // Add delegates for all functionality you want to make available through ViewA
    public ObservableList<String> getItems() { return comboBox.getItems(); }
    public void setOnButton1Action(...) { ... }
    public void setOnButton2Action(...) { ... }
    ...
}
公共类视图扩展边框窗格{
组合框组合框;
按钮1;
公众视野a(){
comboBox=新的comboBox();
button1=新按钮(“填充”);
setTop(按钮1);
setBottom(组合框);
}
//为要通过ViewA提供的所有功能添加代理
公共ObservableList getItems(){return comboBox.getItems();}
public void setOnButton1Action(…){…}
公共无效setOnButton2Action(…){…}
...
}
根据您希望通过ViewA管理的内容,您可以选择任意宽或任意窄