简体   繁体   English

如何用黑色背景显示白色文本?

[英]How to display white text with a black background?

I am trying to figure out why my text isn't being displayed here. 我试图弄清楚为什么我的文本没有显示在这里。 I assume it isn't being colored white, and simply doesn't show up against the black background. 我假设它不是白色,并且根本不会在黑色背景下显示。 So, I'm trying to figure out why the background is being colored black, but the text isn't being shown. 因此,我试图弄清楚为什么背景被涂成黑色,而文本却没有显示。

I am using JavaFX 12. 我正在使用JavaFX 12。

How can I color the letters white? 如何将字母涂成白色?

public class MCVE extends Application {

private static Stage gameStage = new Stage();

@Override
public void start(Stage primaryStage) {

   gameStage.setScene(new Scene(createScreen(), 1280.0, 800.0));

   // adjust window settings.
   gameStage.setTitle("my game");
   gameStage.setResizable(false);

   gameStage.show();
}

private Pane createScreen() {

   Text welcome = new Text("Welcome,");
   welcome.setFont(new Font("Old_Style", 22.0));
   welcome.setStyle("-fx-text-fill: whitesmoke");

   Text toMyGame = new Text(" to my game.");
   toMyGame.setFont(new Font("System", 14.0));
   toMyGame.setStyle("-fx-text-fill: whitesmoke");

   TextFlow tf = createTextFlow();
   ObservableList list = tf.getChildren();

   list.addAll(welcome, toMyGame);

   return tf;
}

private TextFlow createTextFlow() {

   String style = "-fx-background-color: black;";
   style += " -fx-text-fill: whitesmoke;";

   TextFlow tf = new TextFlow();
   tf.setStyle(style);

   return tf;
}

}

-fx-text-fill is a CSS property of Labeled , not Text. -fx-text-fill 是Labeled的CSS属性 ,而不是Text。 Text is a type of shape, and Shape supports -fx-fill rather than -fx-text-fill. 文本是形状的一种,并且Shape支持-fx-fill而不是-fx-text-fill。

Changing every occurrence of -fx-text-fill to -fx-fill should give you the appearance you want. 将每次出现的-fx-text-fill更改为-fx-fill应该会为您提供所需的外观。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM