简体   繁体   English

在JavaFX和MVC(非FXML)中处理Controller中的事件

[英]Handling events in Controller in JavaFX and MVC (Not FXML)

I'm learning JavaFx now and MVC and I'm trying to connect my button to the model through the Controller. 我现在正在学习JavaFx和MVC,并且试图通过Controller将我的按钮连接到模型。 I tried many ways to use button.setOnActive(EventHandler e); 我尝试了多种方法来使用button.setOnActive(EventHandler e); but it doesn't work outside the view class so what is the proper way to do it? 但是它在视图类之外不起作用,那么正确的方法是什么?

this is my View class 这是我的View班

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

and this is my Controller 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**

}

I know that I should connect the button to a method in the model but right now I just want to know how I can make it work. 我知道我应该将按钮连接到模型中的方法,但是现在我只想知道如何使它起作用。

My model 我的模特

public class Model {
private int calculationValue;

public  void addTwoNumbers(int firstNumber, int secondNumber){

    calculationValue = firstNumber + secondNumber;
}

public int getCalculationValue() {
    return calculationValue;
}

} }

this is my main too 这也是我的主要

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

Create a blank project with a ControllerA, ControllerB, Driver, ViewA, and ViewB classes using the code below, then run the code from the main method within the Driver class. 使用下面的代码用ControllerA,ControllerB,Driver,ViewA和ViewB类创建一个空白项目,然后从Driver类中的main方法运行代码。

This should hopefully show you how events can be handled by a controller. 希望它可以向您显示控制器如何处理事件。

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

ControllerA: 控制器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;
    }
}

ViewA: ViewA:

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

ControllerB: 控制器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;
    }
}

View B: 查看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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM