如何更改javafx TextField中文本的颜色?

Sae*_*umi 26 javafx

我想在TextField中更改字体颜色.我找到了-fx-background-color,-fx-border-color用于更改背景和边框的颜色,但没有用于文本.有任何想法吗?谢谢你的建议

Mar*_*fer 34

-fx-text-fill为我设定作品.

见下文:

if (passed) {
    resultInfo.setText("Passed!");
    resultInfo.setStyle("-fx-text-fill: green; -fx-font-size: 16px;");
} else {
    resultInfo.setText("Failed!");
    resultInfo.setStyle("-fx-text-fill: red; -fx-font-size: 16px;");
}
Run Code Online (Sandbox Code Playgroud)


jew*_*sea 25

文本输入控件的CSS样式(如TextField for JavaFX 8)在modena.css样式表中定义,如下所示.创建自定义CSS样式表并根据需要修改颜色.如果您需要帮助理解语法和可用属性和值,请使用CSS参考指南.

.text-input {
    -fx-text-fill: -fx-text-inner-color;
    -fx-highlight-fill: derive(-fx-control-inner-background,-20%);
    -fx-highlight-text-fill: -fx-text-inner-color;
    -fx-prompt-text-fill: derive(-fx-control-inner-background,-30%);
    -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-cursor: text;
    -fx-padding: 0.333333em 0.583em 0.333333em 0.583em; /* 4 7 4 7 */
}
.text-input:focused {
    -fx-highlight-fill: -fx-accent;
    -fx-highlight-text-fill: white;
    -fx-background-color: 
        -fx-focus-color,
        -fx-control-inner-background,
        -fx-faint-focus-color,
        linear-gradient(from 0px 0px to 0px 5px, derive(-fx-control-inner-background, -9%), -fx-control-inner-background);
    -fx-background-insets: -0.2, 1, -1.4, 3;
    -fx-background-radius: 3, 2, 4, 0;
    -fx-prompt-text-fill: transparent;
}
Run Code Online (Sandbox Code Playgroud)

尽管使用外部样式表是进行样式设置的首选方法,但您可以使用以下内容进行内联样式:

textField.setStyle("-fx-text-inner-color: red;");
Run Code Online (Sandbox Code Playgroud)


小智 6

如果您正在使用SceneBuilderthen设计 Javafx 应用程序-fx-text-fill(如果不可用作为选项,则将其写在样式输入框中)作为样式并提供您想要的颜色,它将更改Textfield.

我来这里是为了同样的问题,并以这种方式解决了它。