java JavaFX 更改 CSS 中禁用文本字段的文本颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28604650/
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-02 13:53:04  来源:igfitidea点击:

JavaFX Change textcolor of disabled textfield in CSS

javajavafx

提问by Hans

I have a texfield in a stage:

我在一个阶段有一个 texfield:

@FXML
private TextField tfAdUsername;

tfAdUsername.setPromptText(userName);
tfAdUsername.setDisable(true);

The textcolor is lightgray, i want to change it to black:

文本颜色为浅灰色,我想将其更改为黑色:

.text-field:readonly {
   -fx-background-color: #E0E0E2;
   -fx-border-color: #94BBDA;
   -fx-text-fill: #000000;
}

.text-field:disabled {
   -fx-background-color: #E0E0E2;
   -fx-border-color: #94BBDA;
   -fx-text-fill: #000000;
}

This doesn't change the textcolor. What is the correct CSS Property to use?

这不会改变文本颜色。要使用的正确 CSS 属性是什么?

回答by ItachiUchiha

The reason why color turns grey on disabling is because of the opacity change. Just try adding the following css to your textfield.

颜色在禁用时变为灰色的原因是不透明度的变化。只需尝试将以下 css 添加到您的文本字段。

-fx-opacity: 1.0;

Working example(using setStyle())

工作示例(使用setStyle()

public class KeyStroke extends Application {

    @Override
    public void start(Stage primaryStage) {
        Pane root = new Pane();
        TextField textField = new TextField("Itachi");
        textField.setDisable(true);
        textField.setStyle("-fx-opacity: 1.0;");
        root.getChildren().add(textField);
        Scene scene = new Scene(root, 200, 200);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }
}

回答by WonderWorld

For changing the opacity use:

要更改不透明度,请使用:

.text-input:disabled {
-fx-opacity: 1.0;
}

in the css file.

在 css 文件中。



To change the textcolor used (after i read the question properly i know this wasn't asked.)

要更改使用的文本颜色(在我正确阅读问题后,我知道没有问这个问题。)

From the modena.css stylesheet:

从 modena.css 样式表:

.root {        
     /* Used for the inside of text boxes, password boxes, lists, trees, and
 * tables.  See also -fx-text-inner-color, which should be used as the
 * -fx-text-fill value for text painted on top of backgrounds colored
 * with -fx-control-inner-background.
 */
-fx-control-inner-background: derive(-fx-base,80%);
/* Version of -fx-control-inner-background for alternative rows */
-fx-control-inner-background-alt: derive(-fx-control-inner-background,-2%);

/* One of these colors will be chosen based upon a ladder calculation
 * that uses the brightness of a background color.  Instead of using these
 * colors directly as -fx-text-fill values, the sections in this file should
 * use a derived color to match the background in use.  See also:
 *
 * -fx-text-base-color for text on top of -fx-base, -fx-color, and -fx-body-color
 * -fx-text-background-color for text on top of -fx-background
 * -fx-text-inner-color for text on top of -fx-control-inner-color
 * -fx-selection-bar-text for text on top of -fx-selection-bar
 */
}
.root {        
     /* Used for the inside of text boxes, password boxes, lists, trees, and
 * tables.  See also -fx-text-inner-color, which should be used as the
 * -fx-text-fill value for text painted on top of backgrounds colored
 * with -fx-control-inner-background.
 */
-fx-control-inner-background: derive(-fx-base,80%);
/* Version of -fx-control-inner-background for alternative rows */
-fx-control-inner-background-alt: derive(-fx-control-inner-background,-2%);

/* One of these colors will be chosen based upon a ladder calculation
 * that uses the brightness of a background color.  Instead of using these
 * colors directly as -fx-text-fill values, the sections in this file should
 * use a derived color to match the background in use.  See also:
 *
 * -fx-text-base-color for text on top of -fx-base, -fx-color, and -fx-body-color
 * -fx-text-background-color for text on top of -fx-background
 * -fx-text-inner-color for text on top of -fx-control-inner-color
 * -fx-selection-bar-text for text on top of -fx-selection-bar
 */
}
.root {
    -fx-text-inner-color: #FF01F3;
}

For example:

例如:

   @Override
    public void start(Stage primaryStage) {
    VBox root = new VBox();
    root.setAlignment(Pos.CENTER);
    Button btn = new Button();
    TextField text= new TextField();
    btn.setText("Press me!");
    btn.setOnAction((ActionEvent event) -> {
        text.setText("Goodbye World");
    });
    root.getChildren().addAll(btn, text);   
    Scene scene = new Scene(root, 300, 250);
    scene.getStylesheets().addAll(getClass().getResource("css.css").toExternalForm());
    primaryStage.setScene(scene);
    primaryStage.show();
}

And the color of the text in the textfield will be pink?purple?

文本字段中文本的颜色将是粉红色?紫色?

This is by the way not a means to change the color for each textfield individually, if you change this for the root then all the textfields will use this color instead of the usual black. I don't know how to change the color for one textfield to blue and for another one in the same application to red.

顺便说一下,这不是单独更改每个文本字段的颜色的方法,如果您为根更改此颜色,则所有文本字段都将使用此颜色而不是通常的黑色。我不知道如何将一个文本字段的颜色更改为蓝色,并将同一应用程序中的另一个文本字段的颜色更改为红色。