0

我正在使用 Popover,它用作文本字段的类似工具提示的帮助显示。它包含一个 Label 和一个 TextArea 作为内容,并在用户输入文本字段时创建。(通过FocusPropery.addListener

我使用以下方式应用样式:

popOver.getRoot().getStylesheets().add(...) 

(如文档文档中所示)

这适用于 TextArea,但仅适用于标签。

我的风格是这样的:

*{
    -tb-dark-grey: rgb(32,38,44);
}

.root {
   -fx-base: -tb-dark-grey;
   -fx-background: -tb-dark-grey;
   -fx-control-inner-background: -tb-dark-grey;
}

这在我的主窗口中效果很好。包括所有标签和文本区域。一切都有深蓝色背景和白色文本。但是,对于 Popover 中的标签,它仅将文本颜色更改为白色,但背景保持通常的浅灰色。

我尝试使用 TextArea 作为解决方法。这适用于风格。但它总是从文本字段中窃取焦点。这使得无法输入内容。禁用 TextArea 有效,但这会改变 TextArea 的样式。

我已经尝试应用在这个其他问题中找到的样式。我还尝试重新获得焦点,但也没有用。

popup.Show(this.inputField)
this.inputField.requestFocus(); // also tried this with Platform.runLater
4

1 回答 1

0

您的问题应该是Label不使用您在.root样式类中覆盖的任何颜色。根据JavaFX CSS 参考指南,您可以Label使用fx-background-color.

将以下行添加到您的样式表中应该可以解决问题:

.label {
    -fx-background-color: -tb-dark-grey;
}

如果您想以不同的方式设置标签的样式,还可以通过创建自定义样式类为每个标签单独应用样式:

CSS:

.custom-label {
    -fx-background-color: -tb-dark-grey;
}

然后将其应用于特定标签:

Label label = new Label("I am a label");
label.getStyleClass().add("custom-label");

编辑:您可能应该知道,您TextArea不会显示您在样式表中定义的确切颜色。如果您检查 Modena 样式表,它是 JavaFX 的默认主题和样式(如何找到它在此处描述)。您将找到以下 css 的TextArea内容:

.text-area .content {
    /*the is 1px less top and bottom than TextInput because of scrollpane border */
    -fx-padding: 0.25em 0.583em 0.25em 0.583em; /* 3 7 3 7 */
    -fx-cursor: text;
    -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;
}

如你看到的。内容的背景颜色TextArea并不完全-fx-control-inner-background是您在样式表中定义的颜色,而是从派生-fx-control-inner-background的颜色到您想要的颜色的线性渐变。这对您来说甚至可能并不明显,但很高兴知道。

设置TextArea背景的颜色,这样你的颜色就可以这样完成:

.text-area .content {
    -fx-background-color: tb-dark-grey;
}
于 2016-04-25T14:37:28.847 回答