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

时间:2014-07-11 16:36:10

标签: javafx

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

3 个答案:

答案 0 :(得分:33)

设置-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;");
}

答案 1 :(得分:23)

TextField for JavaFX 8等文本输入控件的CSS样式在modena.css样式表中定义如下。创建自定义CSS样式表并根据需要修改颜色。如果您需要帮助了解语法和可用的属性和值,请使用CSS reference guide

.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;
}

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

textField.setStyle("-fx-text-inner-color: red;");

答案 2 :(得分:0)

如果您使用SceneBuilder设计Javafx应用程序,则使用-fx-text-fill(如果不可用,则将其写在样式输入框中)作为样式并提供所需的颜色,它将更改Textfield的文本颜色。

我也是出于同样的问题来到这里,并以这种方式解决了。