在 JavaFX 和 MVC(不是 FXML)的控制器中处理事件

Handling events in Controller in JavaFX and MVC (Not FXML)

我现在正在学习 JavaFx 和 MVC,我正在尝试通过控制器将我的按钮连接到模型。我尝试了很多方法来使用 button.setOnActive(EventHandler e); 但它在视图之外不起作用 class 那么正确的做法是什么?

这是我的观点class

public class View extends Application{


private TextField num1;
private Label addLabel;
private TextField num2;;
public Button calcB;
private TextField sol;

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("Calculator");

    num1 = new TextField();
    addLabel = new Label("+");
    num2 = new TextField();

    calcB = new Button("Calculate");


    **// I'm trying to use this in the Controller
    calcB.setOnAction(event -> System.exit(0));**

    sol = new TextField();

    FlowPane flowPane = new FlowPane();
    flowPane.getChildren().addAll(num1, addLabel, num2, calcB, sol);

    Scene scene = new Scene(flowPane, 600, 200);
    primaryStage.setScene(scene);
    primaryStage.show();


}

public int getNum1(){
    return Integer.parseInt(num1.getText());
}

public int getNum2(){
    return Integer.parseInt(num2.getText());
}

public void setSol(int sol){
    this.sol.setText(Integer.toString(sol));
}

这是我的控制器Class

public class Controller{


private View  theView;
private Model theModel;

public Controller(Button b, EventHandler<ActionEvent> e) {
    this.theView = theView;
    this.theModel = theModel;

    **//SetOnActive() method should be somewhere in this Class**

}

我知道我应该将按钮连接到模型中的方法,但现在我只想知道如何让它工作。

我的模型

public class Model {
private int calculationValue;

public  void addTwoNumbers(int firstNumber, int secondNumber){

    calculationValue = firstNumber + secondNumber;
}

public int getCalculationValue() {
    return calculationValue;
}

}

这也是我的主菜

public class Main {
public static void main(String[] args){
    View view = new View();
    Model model = new Model();
    Controller controller = new Controller(view, model);

    Application.launch(View.class, args);
}

使用下面的代码创建一个包含 ControllerA、ControllerB、Driver、ViewA 和 ViewB classes 的空白项目,然后 运行 来自 main 方法中的代码Driver class.

这应该能向您展示控制器如何处理事件。

Driver:

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Driver extends Application {
    public static void main(final String[] args) {
        launch();
    }

    @Override
    public void init() {}

    @Override
    public void start(final Stage primaryStage) throws Exception {
        final Scene scene = new Scene(new ControllerA(primaryStage).getView());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

控制器A:

import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class ControllerA implements EventHandler {
    private final Stage primaryStage;
    private final ViewA view = new ViewA(this);

    public ControllerA(final Stage primaryStage) {
        this.primaryStage = primaryStage;
    }

    @Override
    public void handle(final Event event) {
        final Object source = event.getSource();

        if (source.equals(view.getButton())) {
            System.out.println("ButtonA has been pressed, switching to ViewB.");

            final ControllerB controllerB = new ControllerB(primaryStage);
            final Scene scene = new Scene(controllerB.getView());
            primaryStage.setScene(scene);
        }
    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public ViewA getView() {
        return view;
    }
}

视图A:

import javafx.scene.control.Button;
import javafx.scene.layout.HBox;

public class ViewA extends HBox {
    private final Button button = new Button("ButtonA");

    public ViewA(final ControllerA controllerA) {
        button.setOnAction(controllerA);
        this.getChildren().addAll(button);
    }

    public Button getButton() {
        return button;
    }
}

控制器B:

import javafx.stage.Stage;

public class ControllerB {
    private final Stage primaryStage;
    private final ViewB view = new ViewB(this);

    public ControllerB(final Stage primaryStage) {
        this.primaryStage = primaryStage;
    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public ViewB getView() {
        return view;
    }
}

视图 B:

import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ViewB extends HBox {
    private final Button button = new Button("ButtonB");

    public ViewB(final ControllerB controllerB) {
        button.setOnAction(event -> {
            System.out.println("ButtonB has been pressed, switching to ViewA.");

            final Stage primaryStage = controllerB.getPrimaryStage();
            final ControllerA controllerA = new ControllerA(primaryStage);
            final Scene scene = new Scene(controllerA.getView());
            primaryStage.setScene(scene);
        });
        this.getChildren().addAll(button);
    }

    public Button getButton() {
        return button;
    }
}