当前位置: 首页 > 知识库问答 >
问题:

从JavaFX中移除所有焦点边框

暴博远
2023-03-14

在使用JavaFX时,我发现焦点边框在一些可视化节点上存在障碍,比如按钮和某些窗格。关于这个主题的其他问题经常建议在样式文件中添加以下css(覆盖modena.css的默认值,JavaFX8的默认样式表):

-fx-focus-color: transparent;
-fx-faint-focus-color: transparent;

我还发现,这个背景色来自前面提到的绘制边框的属性。因此,将颜色设置为透明会产生意想不到的效果,即某些节点(如窗格、组合框等)在不聚焦时显示边框,但在聚焦时不再显示边框,这是由于:focust伪类的背景色派生方式造成的。

是否可以移除焦点边框(和模糊焦点边框),这样当它们实际上聚焦时,它们仍然保持未聚焦元素的外观?

共有1个答案

胥博文
2023-03-14

我编译了一个解决方案,通过重写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;
}

基本上,不管UI节点是否有焦点,它都会改变背景颜色的呈现,以绘制相同的外观和感觉。

 类似资料:
  • 问题内容: 如何从网络应用程序中嵌入的iframe中删除边框?iframe的一个示例是: 假设背景颜色一致,我希望从页面上的内容到iframe的内容的过渡是无缝的。目标浏览器仅是IE6,遗憾的是,其他浏览器的解决方案将无济于事。 问题答案: 添加属性(注意 大写字母’B’ )。 因此,它看起来像:

  • 我用的是sceneBuilder。 如何清除锚泊烷 有没有办法删除AnchorPane中包含的所有节点(已经从sceneBuilder创建并填充了按钮和形状)?

  • 如何从kubernetes节点中删除所有内容? 内容包括部署、副本集等。我试图单独删除deplyoments。但是kubernetes重新创建了所有的豆荚。 有什么方法可以删除节点中存在的所有副本集吗?

  • 我有一个子组件,它有几个带有以下CSS属性的选项卡: 当我点击后退按钮时,它会返回到上一个选择的选项卡,这很好,但是焦点不会从当前的选项卡中移除,除非我点击屏幕的任何其他部分。 如何去除焦点?

  • 问题内容: 如前所述,我想更改内的默认TAB行为(以使其充当类似或类似的组件) 这是事件动作 这是听众 我也尝试过evt.KEY_TYPED,没有任何喜悦。 有任何想法吗? 快速编辑:我也尝试代替 问题答案: 根据此类: 请注意,可以更短,根据[约书亚·戈德堡的意见,因为我们的目标是获得通过重写回默认行为: 这用于问题“ 我如何修改标签中的Tab键的行为JTextArea? ” 在以前的实现确实是

  • 我正在制作看板,我有一列在里面,它用一个网格窗格划分在标题和下面的卡片列表之间。我正在使用场景生成器构建此场景。 所以层次结构是 当我按下卡上的按钮时,我希望它移除我单击的卡。 我已经做了以下工作 然而,当我按下按钮时什么也没有发生,卡没有被删除,我也不知道为什么。如果有人能帮我,那就太好了。