這有點難看。您的Spinner
(推測)是Spinner<Double>
,所以它有一個ReadOnlyObjectProperty<Double>
valueProperty()
。算術計算方法multiply()
,divide()
等定義爲ReadOnlyDoubleProperty
,而不是ReadOnlyObjectProperty<Double>
,因此您需要將屬性轉換爲ReadOnlyDoubleProperty
。有一個靜態的ReadOnlyDoubleProperty.readOnlyDoubleProperty(ReadOnlyObjectProperty<Double>)
method可以爲你做這個轉換。
所以,你需要做這樣的事情
DoubleBinding total = totalHtProperty.multiply(ReadOnlyDoubleProperty
.readOnlyDoubleProperty(taxSpinner.valueProperty()).divide(100));
然後當然
totalWithTVA.textProperty().bind(total.asString());
其中totalHtProperty
是某種DoubleExpression
與在totalHt
標籤顯示的值(我假設你正在計算這個地方)。
這裏有一個SSCCE:
import javafx.application.Application;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Spinner;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class BoundSpinners extends Application {
@Override
public void start(Stage primaryStage) {
Spinner<Double> taxSpinner = new Spinner<>(0.0, 100.0, 6.0, 0.5);
Spinner<Double> priceSpinner = new Spinner<>(0, Double.MAX_VALUE, 0, 10);
DoubleBinding taxAmount =
ReadOnlyDoubleProperty.readOnlyDoubleProperty(priceSpinner.valueProperty())
.multiply(ReadOnlyDoubleProperty.readOnlyDoubleProperty(taxSpinner.valueProperty()).divide(100));
Label taxLabel = new Label();
taxLabel.textProperty().bind(taxAmount.asString("$%.2f"));
GridPane root = new GridPane();
root.addRow(0, new Label("Price:"), priceSpinner);
root.addRow(1, new Label("Tax rate:"), taxSpinner);
root.addRow(2, new Label("Tax due:"), taxLabel);
root.setAlignment(Pos.CENTER);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
你的描述有點難以遵循。請提供一個[MCVE](http://stackoverflow.com/help/mcve) – hotzst
另外,爲什麼你會同時創建'totalSpinner'和'totalWithTax' spinners?你永遠不希望用戶改變這些值,所以爲什麼不把它們標籤? –