Модели не должны иметь каких-либо ссылок на контроллер (ы) или на вид (ы). Вы должны сделать свойства модели наблюдаемыми, чтобы заинтересованные клиенты (контроллер или представление) могли наблюдать за ними и получать уведомления при их изменении.
Лучшим способом изменения пользовательского интерфейса в JavaFX является использование 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 ;
}
}
Я думаю, что это поможет или даже решение: HTTP: // stackoverflow.com/questions/40117925/javafx-many-static-fxml-controllers – GOXR3PLUS
Модель не должна ссылаться ни на один e (ы) или контроллер (ы). –