文本对象拒绝改变颜色尽管CSS指令这样做

时间:2014-01-04 02:21:26

标签: java css javafx-2

尽管在设置文本之前设置了id,但事件处理程序之前的代码中的Text对象并未更改颜色。但是,文本将使用文本集填充更改颜色。我很好奇的一件事是,在CSS中,颜色不会随着.text{} OR #Sign-In-Text{}而改变。我不正确地使用CSS吗?

Java代码:

public class CSSTrial1 extends Application {

@Override
public void start(Stage stage) throws Exception {
    stage.setTitle("CSS Trial");
    stage.setResizable(false);
    final GridPane grid = new GridPane();
    ColumnConstraints constraints = new ColumnConstraints();
    grid.getColumnConstraints().add(constraints);
    grid.setAlignment(Pos.CENTER);
    grid.setHgap(10);
    grid.setVgap(10);
    grid.setPadding(new Insets(25));
    grid.add(new Label("Character Name:"), 0, 0);
    final TextField nameField = new TextField("");
    nameField.setPromptText("Randel James");
    grid.add(nameField, 1, 0);
    grid.add(new Label("Weapon:"), 0, 1);
    final ComboBox<String> weapons = new ComboBox<>();
    weapons.getItems().addAll("Katsuragi x2", "Assault Rifle",
            "Pistol", "RPG-7", "Barret 50. Cal");
    grid.add(weapons, 1, 1);
    HBox box = new HBox(10);
    box.setAlignment(Pos.BOTTOM_RIGHT);
    Button submit = new Button("Submit Character");
    final Text text = new Text();
    text.setId("Sign-In-Text");
    submit.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent t) {
            text.setText("Signed In As: " + nameField.getText()
                    + ", Weapon: " + weapons.getValue());
            //text.setWrappingWidth(100);
        }
    });
    //text.setFill(Color.ROYALBLUE);
    grid.add(text, 0, 6);
    box.getChildren().add(submit);
    grid.add(box, 1, 3);
    Scene scene = new Scene(grid, 300, 275);
    scene.getStylesheets().add(CSSTrial1.class.getResource("CSSTrial1Style.css")
            .toExternalForm());
    stage.setScene(scene);
    stage.show();
}

}

CSS:

.root{
-fx-background-color: #710ad1;
}
.label{
-fx-text-fill:#11d30b;
-fx-font: bold 13pt fantasy;
}
.text{
-fx-text-fill:#11d30b;
-fx-font: bold 13pt fantasy;
}
#custom-text{
-fx-font: 16px "Serif";
-fx-padding: 10;
-fx-background-color: #CCFF99;
}
#Sign-In-Text{
-fx-text-fill:#11d30b;
-fx-font: bold 10pt fantasy;
}

1 个答案:

答案 0 :(得分:4)

尝试:

-fx-fill: #11d30b; 

而不是:

-fx-fill-text: #11d30b;

文档中的示例:http://docs.oracle.com/javafx/2/text/jfxpub-text.htm#CHDDAEBG

另请参阅此处的类似问题:JavaFX TableView: Cannot Override '-fx-text-fill'!