not working ccs style javafx -fx-strikethrough

后端 未结 2 741
礼貌的吻别
礼貌的吻别 2021-01-24 00:49

I want to make text strikethrough using CSS.

I use tag:

button.setStyle(\"-fx-strikethrough: true;\");

But this code is working, please

2条回答
  •  [愿得一人]
    2021-01-24 01:24

    You need to use a CSS stylesheet to enable strikethrough on a button. Using a CSS stylesheet is usually preferable to using an inline CSS setStyle command anyway.

    /** file: strikethrough.css (place in same directory as Strikeout) */
    .button .text {
        -fx-strikethrough: true;
    }
    

    The CSS style sheet uses a CSS selector to select the text inside the button and apply a strikethrough style to it. Currently (as of Java 8), setStyle commands cannot use CSS selectors, so you must use a CSS stylesheet to achieve this functionality (or use inline lookups, which would not be advisable) - the style sheet is the best solution anyway.

    See @icza's answer to understand why trying to set the -fx-strikethrough style directly on the button does not work.

    strikethrough

    Here is a sample application:

    import javafx.application.Application;
    import javafx.geometry.Insets;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Stage;
    
    public class Strikeout extends Application {
    
        @Override 
        public void start(Stage stage) throws Exception {
            Button strikethrough = new Button("Strikethrough");
            strikethrough.getStylesheets().addAll(getClass().getResource(
                    "strikethrough.css"
            ).toExternalForm());
    
            StackPane layout = new StackPane(
                    strikethrough
            );
            layout.setPadding(new Insets(10));
            stage.setScene(new Scene(layout));
    
            stage.show();
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
    

提交回复
热议问题