为了测试Javafx中的属性绑定,我创建了两个TextField
,如下所示:
public class BindingTest extends Application {
public void start(Stage stage) throws Exception {
TextField text1 = new TextField();
TextField text2 = new TextField();
// text1.textProperty().bindBidirectional(text2.textProperty());
text1.textProperty().bind(text2.textProperty());
VBox root = new VBox(text1, text2);
stage.setTitle("Binding Test");
stage.setScene(new Scene(root, 400, 300));
stage.show();
}
public static void main (String[] args) {
launch(args);
}
}
当我使用双向绑定时,我可以在两个文本字段中键入,并且文本属性绑定非常有效。但是,当我使用单向绑定时,当我键入文本字段2时,文本字段1确实会更新其内容,但我不能再键入文本字段1了。
这正常吗?
是的,您需要使用双向绑定。
如果使用单向绑定,则表示text1中的值必须与text2中的值相同,如果可以键入,则情况并非如此。