有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaFX(带FXML)MVC:模型使用控制器

我对MVC和JavaFX的概念相当陌生。 基本上我有一个游戏,里面有一些简单的动画。为了更好地理解我的问题,这里有一个类比

型号:型号。爪哇

public int fib(int n){
    ctrl.setLabelFib(n)
    someWaitFunction(1000 ms); //Now I can see the changes

    if (n <= 1) return n;
    else return fibonacci(n-1) + fibonacci(n-2);
}

控制器:ctrl。爪哇

Label lFib = new Label();
public void setLabelFib(int n){
   lFib.setText(n)
}

视图:视图。fxml

*Generate with Scene Builder*

如何在递归函数中访问控制器?还是我完全做错了


共 (2) 个答案

  1. # 1 楼答案

    模型不应引用控制器或视图。您应该使模型的属性可观察,以便感兴趣的客户机(控制器或视图)可以观察它们,并在它们更改时收到通知

    在JavaFX中定期更改UI的最佳方法是使用动画API,例如a Timeline

    所以你可以

    private class FibonacciModel {
    
        private final IntegerProperty currentValue = new SimpleIntegerProperty(1);
    
        private int previous = 0 ;
    
        public IntegerProperty currentValueProperty() {
            return currentValue ;
        }
    
        public final int getCurrentValue() {
            return currentValueProperty().get();
        }
    
        public final void setCurrentValue(int value) {
            currentValueProperty().set(value);
        }
    
        public void nextValue() {
            int next = previous + getCurrentValue();
            previous = getCurrentValue();
            setCurrentValue(next);
        }
    }
    

    然后你的观点就可以了

    public class FibonacciView extends VBox {
    
        private final Label label ;
        private final Button startButton  ;
    
        public FibonacciView(FibonacciModel model) {
            label = new Label();
            label.textProperty().bind(model.currentValueProperty().asString());
    
            FibonacciController controller = new FibonacciController(model);
    
            startButton = new Button("Start");
            startButton.setOnAction(e -> {
                startButton.setDisable(true);
                controller.animateNumbers(20)
                    .setOnFinished(event -> startButton.setDisable(false));
            });
    
            getChildren().add(label, startButton);
        }
    }
    

    public class FibonacciController {
    
        private final FibonacciModel model ;
    
        public FibonacciController(FibonacciModel model) {
            this.model = model ;
        }
    
        public Animation animateNumbers(int howMany) {
            Timeline timeline = new Timeline(
                newKeyFrame(Duration.seconds(1), event -> model.nextValue());
            );
            timeline.setCycleCount(howMany);
            timeline.play();
            return timeline ;
        }
    }
    
  2. # 2 楼答案

    在实现Initializable的控制器中,将有@override method : initialize

    在这里,您将创建您的模型:

    model md = new model(); //depending of you constructor
    

    要将控制器添加到模型中,可以通过构造函数提交:

    //in initialize of controller
    model md = new model(this); 
    

    //in you ctrl.java
    private ctrl control;
    public model(ctrl control){
        this.control = control
    }
    

    有了这个,你就可以做到:

    public int fib(int n){
        control.setLabelFib(n)
        Thread.sleep(1000);
    
        if (n <= 1) return n;
        else return fibonacci(n-1) + fibonacci(n-2);
    }
    

    我还写了等待的方法

    试着在评论中讲述它是如何工作的