Javafx 在FXML中将fx:id用作CSS id
在FXML中,如果不指定ID(CSS),那么默认情况下使用fx:ID值。我之前的理解是,这两个完全不相交,ID代表CSS,而只有CSS。控制器中@FXML绑定的fx:id 这可以通过一个小测试来演示-三个按钮,第一个是ID,第二个是FX:ID,第三个是两种类型的IDJavafx 在FXML中将fx:id用作CSS id,javafx,javafx-8,Javafx,Javafx 8,在FXML中,如果不指定ID(CSS),那么默认情况下使用fx:ID值。我之前的理解是,这两个完全不相交,ID代表CSS,而只有CSS。控制器中@FXML绑定的fx:id 这可以通过一个小测试来演示-三个按钮,第一个是ID,第二个是FX:ID,第三个是两种类型的ID <VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" pr
<VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Button id="cssid0" mnemonicParsing="false" text="Button" />
<Button fx:id="fxid1" mnemonicParsing="false" text="Button" />
<Button id="cssid2" fx:id="fxid2" mnemonicParsing="false" text="Button" />
</children>
</VBox>
CSS还允许通过fx:ID进行选择
#cssid0 {
-fx-color: red;
}
#fxid1 {
-fx-color: green;
}
#cssid2 {
-fx-color: blue;
}
这似乎不包括在现有的问题,Javadoc或任何其他我可以找到的地方
这个特性非常有用,因为我们只需要指定一个fx:id,它可以用于控制器、CSS和使用testfx进行单元测试
使用这种方法可以吗?或者我是否正在对未记录的行为建立假设,这些假设在以后的版本中可能会发生变化?或者它是在我丢失的某个地方记录的吗?在我看来,它是部分记录的。但乍一看并不那么明显: 将fx:id值指定给元素会在 以后可由变量引用的文档命名空间 取消引用属性 此外,如果对象的类型定义 作为“id”属性,此值也将传递给对象 setId()方法 它应该用“如果对象定义了idProperty并且尚未设置…”来完成。根据相关源代码,第708行左右为:
// Add the value to the namespace
if (fx_id != null) {
namespace.put(fx_id, value);
// If the value defines an ID property, set it
IDProperty idProperty = value.getClass().getAnnotation(IDProperty.class);
if (idProperty != null) {
Map<String, Object> properties = getProperties();
// set fx:id property value to Node.id only if Node.id was not
// already set when processing start element attributes
if (properties.get(idProperty.value()) == null) {
properties.put(idProperty.value(), fx_id);
}
}
...
}
//将值添加到名称空间
如果(fx_id!=null){
名称空间.put(fx_id,value);
//如果该值定义了ID属性,请将其设置为
IDProperty IDProperty=value.getClass().getAnnotation(IDProperty.class);
如果(idProperty!=null){
映射属性=getProperties();
//仅当Node.id未设置时,才将fx:id属性值设置为Node.id
//在处理开始元素属性时已设置
if(properties.get(idProperty.value())==null){
property.put(idProperty.value(),fx_id);
}
}
...
}
我认为在这种行为的基础上发展是可以的
// Add the value to the namespace
if (fx_id != null) {
namespace.put(fx_id, value);
// If the value defines an ID property, set it
IDProperty idProperty = value.getClass().getAnnotation(IDProperty.class);
if (idProperty != null) {
Map<String, Object> properties = getProperties();
// set fx:id property value to Node.id only if Node.id was not
// already set when processing start element attributes
if (properties.get(idProperty.value()) == null) {
properties.put(idProperty.value(), fx_id);
}
}
...
}