如何显示黑底白字?

How to display white text with a black background?

我想弄清楚为什么我的文字没有显示在这里。我假设它没有被涂成白色,并且根本不会出现在黑色背景下。所以,我想弄清楚为什么背景被涂成黑色,但没有显示文本。

我正在使用 JavaFX 12。

如何将字母涂成白色?

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. Text is a type of shape, and Shape supports -fx-fill 而不是 -fx-text-fill.

将每个出现的 -fx-text-fill 更改为 -fx-fill 应该会给您想要的外观。