Javafx 2 对StyleClass使用bindBidirective()方法
我需要在两个文本字段之间建立下一个依赖关系:当我单击其中任何一个时,它们都会更改样式 我试着这样做:Javafx 2 对StyleClass使用bindBidirective()方法,javafx-2,Javafx 2,我需要在两个文本字段之间建立下一个依赖关系:当我单击其中任何一个时,它们都会更改样式 我试着这样做: tb2.styleProperty().bindBidirectional(tb1.styleProperty()); tb1.setOnMouseClicked(new EventHandler<MouseEvent>() { @Override public void handle(MouseEvent t) {
tb2.styleProperty().bindBidirectional(tb1.styleProperty());
tb1.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent t) {
System.out.println(tb1.styleProperty().toString());
System.out.println(tb2.styleProperty().toString());
tb1.getStyleClass().clear();
tb1.getStyleClass().add("green");
System.out.println(tb1.styleProperty().toString());
System.out.println(tb2.styleProperty().toString());
}
});
此外,我还尝试绑定了它们的文本属性,并且它可以工作(当我更改其中一个文本字段中的文本时,另一个文本字段中的文本也会更改):
那么,我在样式属性上做错了什么呢?
Style
和StyleClass
是两个不同的属性<代码>样式包括代码>分隔的属性:值
对,而样式类
包括CSS文件中定义的CSS选择器列表。在你的处理器中,如果你尝试
tb1.setStyle("-fx-background-color: red");
而不是
tb1.getStyleClass().clear();
tb1.getStyleClass().add("green");
您将看到tb2
的背景色也将改变。要完成任务,请尝试将列表更改侦听器添加到styleClass可观察列表中。我可以通过以下方式实现样式绑定:
SimpleStringProperty dynamicProperty = new SimpleStringProperty("-fx-background-color: " + "#d6d6d6");
Text title = new Text("Test");
title.setId("textID");
dynamicProperty.bindBidirectional(title.styleProperty());
当对dynamicProperty对象调用setValue()方法时,它会更新文本对象的样式属性。我建议对两个对象(tb1、tb2)进行双向绑定
所以。。。似乎无法应用和绑定的不是样式,而是StyleClass(似乎TextField没有StyleClass属性)?@Agafonovia是的,StyleClass似乎不可绑定。
tb1.getStyleClass().clear();
tb1.getStyleClass().add("green");
SimpleStringProperty dynamicProperty = new SimpleStringProperty("-fx-background-color: " + "#d6d6d6");
Text title = new Text("Test");
title.setId("textID");
dynamicProperty.bindBidirectional(title.styleProperty());
SimpleStringProperty dynamicProperty = new SimpleStringProperty("-fx-background-color: " + "#d6d6d6");
dynamicProperty.bindBidirectional(tb1.styleProperty());
dynamicProperty.bindBidirectional(tb2.styleProperty());