JavaFX 8 -fx-background css样式选择器

时间:2017-05-03 14:17:28

标签: css javafx-8

我正在尝试创建自定义按钮控件。我创建了MyButton类,从Control扩展如下所示

public class MyButton extends Control {
    private StringProperty textProperty=null;

    public MyButton() {
        this("Button");
    }

    public MyButton(String text) {
        setSkin(new MyButtonSkin(this));
        setText(text);
    }

    public final StringProperty textProperty() {
        if(textProperty==null) {
            textProperty=new SimpleStringProperty();
        }
        return textProperty;
    }

    public final String getText() {
        return textProperty().get();
    }

    public final void setText(String text) {
        textProperty().set(text);
    }
}

正如您在第9行所见,我使用了一个名为MyButtonSkin的皮肤,我很快就会向您展示

public class MyButtonSkin extends SkinBase<MyButton>{
    private final Paint DEFAULT_BACKGROUND_COLOR=Color.GRAY;

    private Background background;

    private Label label;

    public MyButtonSkin(MyButton control) {
        super(control);
        label=new Label();
        label.textProperty().bind(control.textProperty());


        getChildren().add(label);

        initGraphics();
        initListeners();
    }

    private void initGraphics() {
        background=new Background(new BackgroundFill(
                DEFAULT_BACKGROUND_COLOR, new CornerRadii(20), new Insets(0, 0, 0, 0)));
        getSkinnable().setBackground(background);
    }

    private void initListeners() {
        getSkinnable().layoutBoundsProperty().addListener(obs-> {
            Bounds b=getSkinnable().layoutBoundsProperty().get();
            label.resizeRelocate(
                    b.getMinX(), b.getMinY(), b.getWidth(), b.getHeight());
        });

        //handler for mouse pressed and released are omitted because
        // I think they are not important in order to solve my problem

    }

    protected double computePrefWidth(double height, 
            double topInset, double rightInset, double bottomInset, double leftInset) {
        return label.prefWidth(height);
    }
}

这就是按钮的样子

pure button

现在,如果我在场景构建器中添加样式,如

-fx-background-color: yellow

看起来像那样

button with -fx-background-color: yellow style

我面临的问题是为什么-fx-background-color样式选择器更改角落半径和背景插图?同样的事情,如果我只使用-fx-background-radius,它还会将颜色和插图替换为默认值。 不应该-fx-background-color选择器只改变背景颜色(或填充,如javafx调用)?角半径的-fx-background-radius和insets的-fx-background-insets相同?

感谢您的帮助。

0 个答案:

没有答案