从JavaFX删除所有焦点边框

War*_*kst 3 css javafx focus border

使用JavaFX时,我发现焦点边框在某些视觉节点(例如按钮和某些窗格)上受阻。关于此主题的其他问题通常建议向您的样式文件添加以下css(覆盖Modena.css的默认值,Java FX 8的默认样式表)

-fx-focus-color: transparent;
-fx-faint-focus-color: transparent;
Run Code Online (Sandbox Code Playgroud)

乍一看,这消除了焦点边框,但是使用了一段时间后,我发现某些UI元素丢失的不仅仅是蓝色。经过Modena后,我发现这是由于绘制了许多节点的方式所致:其中许多节点具有背景颜色,该背景颜色由多个堆叠在一起的框组成,这些框具有不同的插图和半径,从而形成边框-喜欢看。

我还发现,这种背景色源自前面提到的用于绘制边框的属性。因此,将颜色设置为透明会产​​生意想不到的效果,即某些节点(如窗格,组合框等)在未聚焦时会显示边框,而在聚焦时将不再显示边框,这是因为:focused伪对象的背景色是通过这种方式得出的-类。

是否可以删除焦点边框(和模糊的焦点边框),以使它们在实际上聚焦时仍保留未聚焦元素的外观?

War*_*kst 5

我已经编译了一个解决方案,其中通过覆盖在Modena上的更多部分来结合我在SO上发现的内容,从而消除了焦点边界:我选择了用非焦点对象覆盖那些“:focused”伪类的属性默认为按钮式和窗格式(如Modena本身所述)。结果是:

.button:focused,
.toggle-button:focused,
.radio-button:focused > .radio,
.check-box:focused > .box,
.menu-button:focused,
.choice-box:focused,
.color-picker.split-button:focused > .color-picker-label,
.combo-box-base:focused,
.slider:focused .thumb {
    -fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, -fx-body-color;
    -fx-background-insets: 0 0 -1 0, 0, 1, 2;
    -fx-background-radius: 3px, 3px, 2px, 1px;
}

.scroll-pane:focused,
.split-pane:focused,
.list-view:focused,
.tree-view:focused,
.table-view:focused,
.tree-table-view:focused,
.html-editor:focused {
    -fx-background-color: -fx-box-border, -fx-control-inner-background;
    -fx-background-insets: 0, 1;
    -fx-padding: 1;
}

.radio-button > .radio, .radio-button:focused > .radio  {   
    -fx-background-radius: 1.0em; /* large value to make sure this remains circular */    
    -fx-padding: 0.333333em; /* 4 -- padding from outside edge to the inner black dot */ 
}
.split-menu-button:focused {
    -fx-background-color: -fx-shadow-highlight-color, -fx-outer-border; 
    -fx-background-insets: 0 0 -1 0, 0;
    -fx-background-radius: 3, 3;
}
.split-menu-button:focused > .label {
    -fx-text-fill: -fx-text-base-color;
    -fx-background-color: -fx-inner-border, -fx-body-color; 
    -fx-background-insets: 1 0 1 1, 2 1 2 2;
    -fx-background-radius: 2 0 0 2, 1 0 0 1;
    -fx-padding: 0.333333em 0.667em 0.333333em 0.667em; /* 4 8 4 8 */
}
.split-menu-button:focused > .arrow-button {
    -fx-background-color: -fx-inner-border, -fx-body-color; 
    -fx-background-insets: 1, 2;
    -fx-background-radius: 0 2 2 0, 0 1 1 0;
    -fx-padding: 0.5em 0.667em 0.5em 0.667em; /* 6 8 6 8 */
}

.scroll-bar:focused {
    -fx-background-color: derive(-fx-box-border,30%), linear-gradient(to bottom, derive(-fx-base,-3%), derive(-fx-base,5%) 50%, derive(-fx-base,-3%));
    -fx-background-insets: 0, 1 0 1 0;
}
.scroll-bar:vertical:focused {
    -fx-background-color: derive(-fx-box-border,30%), linear-gradient(to right, derive(-fx-base,-3%), derive(-fx-base,5%) 50%, derive(-fx-base,-3%));
    -fx-background-insets: 0, 0 1 0 1;
}

.text-input:focused {
    -fx-highlight-fill: -fx-accent;
    -fx-highlight-text-fill: white;
    -fx-background-color: linear-gradient(to bottom, derive(-fx-text-box-border, -10%), -fx-text-box-border),
        linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
    -fx-background-insets: 0, 1;
    -fx-background-radius: 3, 2;
    -fx-prompt-text-fill: transparent;
}

.text-area:focused .content {
    -fx-background-color:
        linear-gradient(from 0px 0px to 0px 4px, derive(-fx-control-inner-background, -8%), -fx-control-inner-background);
    -fx-background-radius: 2;
}

.titled-pane:focused > .title > .arrow-button > .arrow {
    -fx-background-color: -fx-mark-highlight-color, -fx-mark-color;
    -fx-background-insets: 1 0 -1 0, 0;
}

.color-picker.split-button:focused > .arrow-button {
    -fx-background-color: -fx-outer-border, -fx-inner-border, -fx-body-color;
    -fx-background-insets: 1 1 1 0, 1, 2;
    -fx-background-radius: 0 3 3 0, 0 2 2 0, 0 1 1 0;
}
Run Code Online (Sandbox Code Playgroud)

基本上,它会更改背景颜色的呈现方式以绘制相同的外观,无论UI节点是否具有焦点。