ccs样式javafx -fx-strikethrough无法正常工作 [英] not working ccs style javafx -fx-strikethrough

查看:123
本文介绍了ccs样式javafx -fx-strikethrough无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CSS制作文本删除线。

I want to make text strikethrough using CSS.

我使用标签:

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

但是此代码有效,请提供帮助。

But this code is working, please help.

推荐答案

您需要使用CSS样式表才能在按钮上启用删除线。无论如何,使用CSS样式表通常比使用内联CSS setStyle命令更为方便。

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;
}

CSS样式表使用CSS选择器选择按钮内的文本,然后对它应用删除线样式。当前(从Java 8开始),setStyle命令不能使用CSS选择器,因此您必须使用CSS样式表才能实现此功能(或使用内联查找,这是不可取的)-无论如何,样式表是最好的解决方案。

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.

请参阅@icza的答案,以了解为什么尝试直接在按钮上设置 -fx-strikethrough 样式无效的原因。

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

这里是一个示例应用程序:

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);
    }
}

这篇关于ccs样式javafx -fx-strikethrough无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆