Невозможно добавить CSS классов к моей метке JavaFX - PullRequest
0 голосов
/ 11 июля 2020

Я делаю простое приложение-todolist. Вот его упрощенная версия:

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        ListView<Label> todoListView = new ListView();

        Label doneTodo = new Label ("This todo is meant to be finished and struck through");
        doneTodo.getStyleClass().add("done"); // Add the "done" class to this to-do
        Label undoneTodo = new Label("This todo is meant to be undone and therefore isn't struck through");

        // Add both to-dos to the listview
        addTodo(doneTodo, todoListView);
        addTodo(undoneTodo, todoListView);

        // Set the listview as the scene, and add the stylesheet
        Scene scene = new Scene(todoListView, 600, 550);
        scene.getStylesheets().add(getClass().getResource("styles.css").toExternalForm());

        primaryStage.setTitle("Label not taking on Strikethrough");
        primaryStage.setScene(scene);
        primaryStage.show();


    }

    // Adds the to-do (in this case just a simple Label) to the listview
    private static void addTodo(Label todo, ListView<Label> todoList) {
        todoList.getItems().add(todo);
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Вот мой CSS класс:

.done {
    -fx-strikethrough: true;
}

Когда я запускаю код, свойство -fx-strikethrough не отображается на моем лейбле. Обратите внимание, что хотя это упрощенная версия моего приложения, проблема остается прежней: текст внутри метки JavaFX не зачеркнут.

Опять же, извините за любые несоответствия в моем вопросе. Я новичок в Stack Overflow!

Заранее спасибо.

1 Ответ

0 голосов
/ 12 июля 2020

Правило CSS должно применяться к текстовому узлу под узлом метки:

.done .text {
    -fx-strikethrough: true;
}
...