2

我正在研究通过 CSS 自定义控件,但我已经走得很远了。所以我可以完全自定义我的滚动条,例如将轨道的背景设置为透明等等。但我坚持使用 ScrollBarSkin(通过 ScenicViewer 进行调查)。这个皮肤似乎有一个默认的背景颜色(渐变)和一个我无法修改的边框。所以我的问题是,我如何访问例如 TableCellSkin 或 ScrollBarSkin,以通过 CSS 修改背景颜色和插图?

编辑:我正在使用 jdk7

edit2:我在 caspian.css 中为 ScrollPaneSkin 找到了一些语法。我对滚动条和表格单元尝试了相同的操作:

 ScrollBarSkin>* {
    -fx-base: transparent;
    -fx-border-color: #00ff00;
    -fx-background-color: #0000ff;
    }

但没有运气。

根据jewelsea的回答找到了解决方案(谢谢队友!)

我创建了一个扩展 ScrollBarSkin 的新类,并覆盖了 getSkinnable()。这看起来像这样:

public class MyScrollBarSkin extends ScrollBarSkin{

    public MyScrollBarSkin(ScrollBar scrollBar) {
        super(scrollBar);

    }

    @Override
    public Insets getInsets() {
        // TODO Auto-generated method stub
        return super.getInsets();
    }

    @Override
    public ScrollBar getSkinnable() {
        ScrollBar curr = super.getSkinnable();
        curr.getSkin().getNode().setStyle("-fx-background-color: transparent;");
        return curr;
    }  
}

在相应的 css 中,我将这个皮肤称为 Jewelsea 提到的。瞧!

还有一个小问题:为什么我不能通过 css 直接访问这个组件?

4

1 回答 1

3

ScrollBarSkin is a class representing the skin used to render the ScrollBar. Here is an extract from a default JavaFX style sheet:

.scroll-bar {
    -fx-skin: "com.sun.javafx.scene.control.skin.ScrollBarSkin";
}

Here is a link to ScrollBarSkin.java in the JavaFX 8 source repository. Note that it is a com.sun class, so it is not part of the public API and could disappear or change API between minor JavaFX releases without notice.

You can override the default skin with your own skin via the following css in your user stylesheet:

.scroll-bar {
    -fx-skin: "com.mycompany.control.skin.CustomScrollBarSkin";
}

I just made the name and path up, you can use whatever you want.

What the skin is allowing is programmatic control over the look of the a control (i.e. it's only incidentally related to css because css is one way to set the skin on a control).

Customizing Skins is documented (to a certain extent) in the OpenJFX wiki.

The skin customization relies on a new JavaFX 8 class called SkinBase, which forms part of the javafx.scene.control public API.

Customizing skins in versions lower than Java 8 is not recommended, because then you will be working with old, undocumented and unsupported private APIs which will not work with Java 8 and later. Customizing skins in Java 8 is fine because it relies on the public API.

I'm pretty sure from your question that this isn't really what you are looking for, but it is the answer to your question (at least as I understood it).

于 2013-07-22T19:41:21.703 回答