我已经创建了我的第一个自定义可修饰控件。由css设置的颜色很好,但是当我试图为背景或边框设置半径时,它不起作用。如何修改这些类以使其与其他维度的CSS属性一起使用?我找到的所有教程都只解释了如何设置新的颜色css属性。你能把我的其他例子联系起来吗?
控制类
public class HeaderPane extends Control {
private static final String DEFAULT_CLASS_NAME = "header-pane";
private static final String BACKGROUND_CSS_PROPERTY = "-fx-header-background-color";
private static final Color DEFAULT_BACKGROUND_COLOR = Color.web("#0188AE");
private final SimpleStringProperty title = new SimpleStringProperty("Header Title");
private ObjectProperty<Color> backgroundColor = new StyleableObjectProperty<Color>(DEFAULT_BACKGROUND_COLOR) {
(..)
};
private final ObjectProperty<EventHandler<ActionEvent>> onAction = new ObjectPropertyBase<EventHandler<ActionEvent>>() {
(...)
};
public HeaderPane() {
initialize();
// createDefaultSkin();
updateBackgroundColor();
}
public HeaderPane(String title) {
this.title.set(title);
initialize();
createDefaultSkin();
updateBackgroundColor();
}
private void initialize() {
getStyleClass().add(DEFAULT_CLASS_NAME);
}
void updateBackgroundColor() {
setBackground(new Background(new BackgroundFill(getBackgroundColor(), null, Insets.EMPTY)));
}
// GETTERS SETTERS
(...)
// . Inner static class Styleable properties
private static class StyleableProperties {
private static final CssMetaData<HeaderPane, Color> STYLEABLES = . (...)
/**
* Create DefaultSkin override
* @return
*/
@Override
protected Skin<?> createDefaultSkin() {
return new HeaderPaneSkin(this);
}
// * Return default Css
@Override
public String getUserAgentStylesheet() { (...) }
}
皮肤类
public class HeaderPaneSkin extends SkinBase<HeaderPane> {
private final Color default_color = Color.web("#0188AE");
@Getter
private final AnchorPane backgroundPane;
private final Label title;
private boolean invalidate = false;
private final InvalidationListener invalidListener = observable -> invalidate = true;
{
backgroundPane = new AnchorPane();
title = new Label();
}
public HeaderPaneSkin(HeaderPane control) {
super(control);
initialize();
getSkinnable().prefWidthProperty().bind(backgroundPane.widthProperty());
getSkinnable().prefHeightProperty().bind(backgroundPane.heightProperty());
getSkinnable().widthProperty().addListener(invalidListener);
getSkinnable().heightProperty().addListener(invalidListener);
AnchorPane.setRightAnchor(title, 20.0);
AnchorPane.setLeftAnchor(title, 20.0);
}
private void initialize() {
getChildren().add(backgroundPane);
backgroundPane.getChildren().addAll(title);
title.textProperty().bind(getSkinnable().titleProperty());
}
}
@fabian
@Override
protected List<CssMetaData<? extends Styleable, ?>> getControlCssMetaData() {
return getClassCssMetaData();
}
public static List<CssMetaData<? extends Styleable, ?>> getClassCssMetaData() {
return StyleableProperties.cssMetaDataList;
}