java JavaFX 组合框 CSS 样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38437700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:24:18  来源:igfitidea点击:

JavaFX ComboBox CSS style

javacssjavafxcombobox

提问by Xendar

I am building a small application with JavaFX + FXML and I'm trying to implement some simple CSS to have a specific style.

我正在使用 JavaFX + FXML 构建一个小型应用程序,并且我正在尝试实现一些简单的 CSS 以具有特定的样式。

I have an issue with the Combobox element. Indeed by default its color is grey:

我对 Combobox 元素有疑问。事实上,默认情况下它的颜色是灰色的:

enter image description here

在此处输入图片说明

And I would like to have it white (or transparent), and keep the borders, to match the same style as the Text Field. So I tried to set the background color to transparent but there is a side effect: The borders become transparent too!

我希望它是白色(或透明),并保留边框,以匹配与文本字段相同的样式。所以我试图将背景颜色设置为透明,但有一个副作用:边框也变得透明了!

enter image description here

在此处输入图片说明

Here is the CSS i have added:

这是我添加的 CSS:

.root {
    -fx-font-size: 11pt;
    -fx-font-family: "Verdana";
    -fx-background: #FFFFFF;
}

.normal-label {
    -fx-text-fill: #005EB8;
}

.normal-text-field {
    -fx-text-fill: #333333;
}

.combo-box {
    -fx-background-color: transparent;
}

I am not at all used to CSS writing, so maybe I completely miss something out. Is it that the combobox does not define borders? So I have to override the borders and find out what are the borders of the Text Field?

我完全不习惯 CSS 写作,所以也许我完全错过了一些东西。是组合框没有定义边框吗?所以我必须覆盖边界并找出文本字段的边界是什么?

回答by DVarga

ComboBoxinherits its CSS stylefrom ComboBoxBase.

ComboBox继承了它的CSS样式ComboBoxBase

The ComboBox control has all the properties and pseudo?classes of ComboBoxBase.

ComboBox 控件具有 ComboBoxBase 的所有属性和伪类。

The default CSS style class of ComboBoxBaseis defined as:

默认的 CSS 样式类ComboBoxBase定义为:

.combo-box-base {
    -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;
    -fx-padding: 0.333333em 0.666667em 0.333333em 0.666667em; /* 4 8 4 8 */
    -fx-text-fill: -fx-text-base-color;
    -fx-alignment: CENTER;
    -fx-content-display: LEFT;
}

You can overwrite this style class like:

您可以像这样覆盖这个样式类:

.combo-box-base {
    -fx-background-color: -fx-shadow-highlight-color, -fx-outer-border, -fx-inner-border, white;
    -fx-background-insets: 0 0 -1 0, 0, 1, 2;
    -fx-background-radius: 3px, 3px, 2px, 1px;
}

This style-class just sets the inner part to white, the border is actually untouched (remove the last two properties and then you will get a plain white borderless ComboBox).

这个样式类只是将内部部分设置为白色,边框实际上没有受到影响(删除最后两个属性,然后您将得到一个纯白色的无边框ComboBox)。



Note:

笔记:

Overwriting .combo-box-baseor .combo-boxstyle-classes are equivalent if only ComboBoxes are used.

如果仅使用es,则覆盖.combo-box-base.combo-box样式类是等效的ComboBox

The reason in the answer to use .combo-box-basestyle-class rather than the other one is that there are other controls inheriting also the .combo-box-basestyle-class, such as ColorPickerand DatePicker. Overwriting .combo-box-baseyields in having all these controls sharing the same style, resulting in a much unified design.

使用.combo-box-basestyle-class 而不是另一个的答案的原因是还有其他控件也继承了.combo-box-basestyle-class,例如ColorPickerand DatePicker。覆盖.combo-box-base会使所有这些控件共享相同的样式,从而形成更加统一的设计。

回答by yamenk

You can add the following properties to control the border:

您可以添加以下属性来控制边框:

-fx-border-color: #D3D3D3
-fx-border-width: 1 1 1 1