JavaFX中的Bind textfield
如何在JavaFx中将一个文本字段与其他文本字段绑定。比如说JavaFX中的Bind textfield,javafx,Javafx,如何在JavaFx中将一个文本字段与其他文本字段绑定。比如说 textFieldTotal.textProperty.bind(Bindings.multiply(textFieldAmount.textProperty, textFieldPrice.textProperty)); 你的问题很模糊,但我要猜一猜。基本上,您需要创建一些附加的属性和绑定对象 首先,为两个TextField节点创
textFieldTotal.textProperty.bind(Bindings.multiply(textFieldAmount.textProperty,
textFieldPrice.textProperty));
你的问题很模糊,但我要猜一猜。基本上,您需要创建一些附加的
属性
和绑定
对象
首先,为两个TextField
节点创建属性。然后,您将使用StringConverter
将输入的文本转换为双精度
最后,创建一个“total”属性,将两个字段相乘,并将它们显示在标签中
下面是一个要测试的非常简单的应用程序:
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.StringConverter;
import javafx.util.converter.DoubleStringConverter;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Simple Interface
VBox root = new VBox(10);
root.setAlignment(Pos.CENTER);
root.setPadding(new Insets(10));
// Quantity
HBox hBoxQuantity = new HBox(5);
hBoxQuantity.setAlignment(Pos.CENTER);
TextField txtQuantity = new TextField();
hBoxQuantity.getChildren().addAll(
new Label("Quantity:"),
txtQuantity
);
// Price
HBox hBoxPrice = new HBox(5);
hBoxPrice.setAlignment(Pos.CENTER);
TextField txtPrice = new TextField();
hBoxPrice.getChildren().addAll(
new Label("Price:"),
txtPrice
);
// Total
HBox hBoxTotal = new HBox(5);
hBoxTotal.setAlignment(Pos.CENTER);
Label lblTotal = new Label();
hBoxTotal.getChildren().addAll(
new Label("Total: $"),
lblTotal
);
// Properties used for bindings
DoubleProperty price = new SimpleDoubleProperty();
DoubleProperty quantity = new SimpleDoubleProperty();
DoubleProperty total = new SimpleDoubleProperty();
// Bind the total to price x quantity
total.bind(price.multiply(quantity));
// Setup the converters to get the input from the textfields
StringConverter<? extends Number> converter = new DoubleStringConverter();
// Bind the text field entries to their properties
Bindings.bindBidirectional(txtPrice.textProperty(), price, (StringConverter<Number>) converter);
Bindings.bindBidirectional(txtQuantity.textProperty(), quantity, (StringConverter<Number>) converter);
// Bind the total label
lblTotal.textProperty().bind(total.asString());
// Add all nodes to stage
root.getChildren().addAll(hBoxPrice, hBoxQuantity, hBoxTotal);
// Show the stage
primaryStage.setWidth(300);
primaryStage.setHeight(300);
primaryStage.setScene(new Scene(root));
primaryStage.setTitle("Sample");
primaryStage.show();
}
}
导入javafx.application.application;
导入javafx.beans.binding.Bindings;
导入javafx.beans.property.DoubleProperty;
导入javafx.beans.property.SimpleDoubleProperty;
导入javafx.geometry.Insets;
导入javafx.geometry.Pos;
导入javafx.scene.scene;
导入javafx.scene.control.Label;
导入javafx.scene.control.TextField;
导入javafx.scene.layout.HBox;
导入javafx.scene.layout.VBox;
导入javafx.stage.stage;
导入javafx.util.StringConverter;
导入javafx.util.converter.DoubleStringConverter;
公共类主扩展应用程序{
公共静态void main(字符串[]args){
发射(args);
}
@凌驾
公共无效开始(阶段primaryStage){
//简单接口
VBox根=新的VBox(10);
根部设置对齐(位置中心);
根。设置填充(新插图(10));
//数量
HBox hBoxQuantity=新的HBox(5);
hBoxQuantity.setAlignment(位置中心);
TextField txtQuantity=新的TextField();
hBoxQuantity.getChildren().addAll(
新标签(“数量:”),
TXT数量
);
//价格
HBox hBoxPrice=新的HBox(5);
hBoxPrice.setAlignment(位置中心);
TextField txtPrice=新的TextField();
hBoxPrice.getChildren().addAll(
新标签(“价格:”),
txtPrice
);
//总数
HBox hBoxTotal=新的HBox(5);
hBoxTotal.setAlignment(位置中心);
Label lblTotal=新标签();
hBoxTotal.getChildren().addAll(
新标签(“总计:$”,
lblTotal
);
//用于绑定的属性
DoubleProperty价格=新的SimpleDoubleProperty();
DoubleProperty数量=新的SimpleDoubleProperty();
DoubleProperty总计=新的SimpleDoubleProperty();
//将总价绑定到数量x价格
合计绑定(价格乘以数量);
//设置转换器以从文本字段获取输入
弦变换器