Java FX 数据绑定demo
立即下载
资源介绍:
将变化的数据统一抽象为IntegerProperty与圆和进度条进行数据绑定,然后add 和sub只管更改IntergerProperty(),最后实现通过addButon和subButton来控制圆和进度条
package com.example.bindingdemon;
import javafx.application.Platform;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.shape.Arc;
import java.net.URL;
import java.util.ResourceBundle;
public class HelloController implements Initializable {
@FXML
public Arc arc;
@FXML
public Label numLabel;
@FXML
public Button addBtn;
@FXML
public Button subBtn;
@FXML
public ProgressBar bar;
IntegerProperty integerProperty = new SimpleIntegerProperty(0);
@Override
public void initialize(URL url, ResourceBundle resourceBundle) {
arc.setStartAngle(90);
arc.setLength(-0);
numLabel.textProperty().bind(integerProperty.asString());
addBtn.disableProperty().bind(integerProperty.greaterThanOrEqualTo(100));
subBtn.disableProperty().bind(integerProperty.lessThanOrEqualTo(0));
integerProperty.addListener((observableValue, number, t1) -> {
arc.setStartAngle(90);
arc.setLength(-t1.intValue() * 3.6);
bar.setProgress(t1.intValue() / 100.0);
});
}
@FXML
public void sub(ActionEvent actionEvent) {
integerProperty.set(integerProperty.get() - 10);
}
@FXML
public void add(ActionEvent actionEvent) {
integerProperty.set(integerProperty.get() + 10);
}
}