我在舞台上有一个文字字段:
@FXML
private TextField tfAdUsername;
tfAdUsername.setPromptText(userName);
tfAdUsername.setDisable(true);
文字颜色为浅灰色,我想将其更改为黑色:
.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;
}
这不会改变文字颜色。什么是正确的CSS属性?
答案 0 :(得分:9)
在禁用时颜色变为灰色的原因是由于不透明度的变化。只需尝试将以下css添加到文本字段中。
-fx-opacity: 1.0;
工作示例(使用 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);
}
}
答案 1 :(得分:2)
更改不透明度使用:
.text-input:disabled {
-fx-opacity: 1.0;
}
在css文件中。
要更改使用的文字颜色(在我正确阅读问题后,我知道这不是问。)
来自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 {
-fx-text-inner-color: #FF01F3;
}
例如:
@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();
}
文本字段中的文字颜色为粉红色?紫色?
顺便说一句,这并不是单独更改每个文本字段颜色的方法,如果您为root更改此颜色,则所有文本字段将使用此颜色而不是通常的黑色。我不知道如何将一个文本字段的颜色更改为蓝色,将另一个文本字段的颜色更改为红色。