JavaFX 中的 StyleableProperty CSS
StyleableProperty in JavaFX CSS
我正在开发一个自定义控件,我正在使用 StyleablePropertyFactory 来尝试制作一个可设置样式的 属性,如下所示:
private static final StyleablePropertyFactory<RackControl> FACTORY = new StyleablePropertyFactory<>(RackControl.getClassCssMetaData());
private StyleableProperty<Color> defaultTubeColor =
FACTORY.createStyleableColorProperty(this, "defaultTubeColor", "-fx-default-tube-color", rc -> rc.defaultTubeColor);
然后我得到了 getter 和 setter,它们允许我访问读入值:
public Color getDefaultTubeColor() {
return defaultTubeColor.getValue();
}
public void setDefaultTubeColor(Color defaultTubeColor) {
System.out.println("Set default tube color");
this.defaultTubeColor.setValue(defaultTubeColor);
}
@SuppressWarnings("unchecked")
public ObservableValue<Color> defaultTubeColorProperty() {
return (ObservableValue<Color>) defaultTubeColor;
}
在构造函数中,我正在设置要注意的外观和样式:
this.setSkin(new RackControlSkin(this));
getStyleClass().add("rack-control");
然后在我的皮肤上(尽管我从哪里得到它并不重要)我正在尝试加载 css 属性 如下:
System.out.println(control.getCssMetaData().stream().filter(c -> c.getProperty().equals("-fx-default-tube-color")).findFirst().get());
System.out.println(control.getDefaultTubeColor());
打印出来:
CSSProperty {property: -fx-default-tube-color, converter: ColorConverter, initalValue: 0x000000ff, inherits: false, subProperties: []}
0x000000ff
所以我知道 属性 是从以下 css 文件中找到的:
.rack-control {
-fx-default-tube-color: red;
}
如我的测试代码所示,从场景调用时:
VBox root = new VBox();
RackControl control = new RackControl(8,12);
root.getChildren().addAll(control);
VBox.setVgrow(control, Priority.ALWAYS);
Scene scene = new Scene(root, 320, 200);
String css = this.getClass().getResource("rackcontrol.css").toExternalForm();
scene.getStylesheets().add(css);
//Scene scene = new Scene((Parent) FXMLLoader.load(getClass().getResource("TestUI.fxml")));
primaryStage.setTitle("Custom control");
primaryStage.setScene(scene);
primaryStage.show();
据我所知;我做的一切都正确,但只是没有从 css 文件加载值。任何人都可以向我提供有关我在这里做错了什么的指示吗?
提前感谢您阅读问题并再次感谢您的任何建议!
干杯,
尼尔
我们通过解构元 类 找到了答案 - css 属性直到 构造函数被调用后才被设置。因此,您需要设置皮肤以听取 css 中的变化,然后进行绘图等
我正在开发一个自定义控件,我正在使用 StyleablePropertyFactory 来尝试制作一个可设置样式的 属性,如下所示:
private static final StyleablePropertyFactory<RackControl> FACTORY = new StyleablePropertyFactory<>(RackControl.getClassCssMetaData());
private StyleableProperty<Color> defaultTubeColor =
FACTORY.createStyleableColorProperty(this, "defaultTubeColor", "-fx-default-tube-color", rc -> rc.defaultTubeColor);
然后我得到了 getter 和 setter,它们允许我访问读入值:
public Color getDefaultTubeColor() {
return defaultTubeColor.getValue();
}
public void setDefaultTubeColor(Color defaultTubeColor) {
System.out.println("Set default tube color");
this.defaultTubeColor.setValue(defaultTubeColor);
}
@SuppressWarnings("unchecked")
public ObservableValue<Color> defaultTubeColorProperty() {
return (ObservableValue<Color>) defaultTubeColor;
}
在构造函数中,我正在设置要注意的外观和样式:
this.setSkin(new RackControlSkin(this));
getStyleClass().add("rack-control");
然后在我的皮肤上(尽管我从哪里得到它并不重要)我正在尝试加载 css 属性 如下:
System.out.println(control.getCssMetaData().stream().filter(c -> c.getProperty().equals("-fx-default-tube-color")).findFirst().get());
System.out.println(control.getDefaultTubeColor());
打印出来:
CSSProperty {property: -fx-default-tube-color, converter: ColorConverter, initalValue: 0x000000ff, inherits: false, subProperties: []}
0x000000ff
所以我知道 属性 是从以下 css 文件中找到的:
.rack-control {
-fx-default-tube-color: red;
}
如我的测试代码所示,从场景调用时:
VBox root = new VBox();
RackControl control = new RackControl(8,12);
root.getChildren().addAll(control);
VBox.setVgrow(control, Priority.ALWAYS);
Scene scene = new Scene(root, 320, 200);
String css = this.getClass().getResource("rackcontrol.css").toExternalForm();
scene.getStylesheets().add(css);
//Scene scene = new Scene((Parent) FXMLLoader.load(getClass().getResource("TestUI.fxml")));
primaryStage.setTitle("Custom control");
primaryStage.setScene(scene);
primaryStage.show();
据我所知;我做的一切都正确,但只是没有从 css 文件加载值。任何人都可以向我提供有关我在这里做错了什么的指示吗?
提前感谢您阅读问题并再次感谢您的任何建议!
干杯,
尼尔
我们通过解构元 类 找到了答案 - css 属性直到 构造函数被调用后才被设置。因此,您需要设置皮肤以听取 css 中的变化,然后进行绘图等