【问题标题】:JavaFX: Remove percentage from ProgressIndicatorJavaFX:从 ProgressIndicator 中删除百分比
【发布时间】:2017-03-10 09:26:37
【问题描述】:

我一直在尝试删除 ProgressIndicator 下方的百分比文本,但没有成功。我发现了几个提到这一点并尝试了接受的答案,但它不起作用

使用 Java 8 更新 121

package com.company.mytest;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class ProgressApp extends Application {

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

    /* (non-Javadoc)
     * @see javafx.application.Application#start(javafx.stage.Stage)
     */
    @Override
    public void start(Stage stage) throws Exception {
        ProgressIndicator progress = new ProgressIndicator();
        progress.setProgress(0.5f);

        StackPane root = new StackPane();
        root.getChildren().add(progress);

        Scene scene = new Scene(root);

        scene.getStylesheets()
        .add(getClass().getResource("progress.css").toExternalForm());

        stage.setScene(scene);
        stage.setWidth(300);
        stage.setHeight(300);
        stage.setTitle("JavaFX 8 app");
        stage.show();
    }

}

我知道 CSS 已加载,因为我可以更改百分比字体大小。这是 progress.css 文件的 CSS 内容。

.progress-indicator .percentage {
    visibility: hidden;
}

【问题讨论】:

    标签: java css javafx javafx-8


    【解决方案1】:

    -fx-fill 属性设置为null 似乎可以解决问题:

    .progress-indicator .percentage {
       -fx-fill:null;
    }
    

    更新:

    仅设置此属性隐藏百分比文本,但仍占用空间。

    一种可能的解决方法是设置ProgressIndicator-fx-padding 属性:

    .progress-indicator .percentage {
        -fx-fill:null;
    }
    
    .progress-indicator  {
        -fx-padding: 0 0 -16 0;
    }
    

    唯一的问题是硬编码值:如果 .progress-indicator .percentage 的 CSS 发生更改(例如更大的字体大小),那么这也必须进行调整。


    此答案中的另一种编程解决方案:How can I avoid the display of percentage values, when using the ProgressIndicator of JavaFX UI Controls

    【讨论】:

    • 是的,但它仍然占用空间。即使将 font-size 设置得非常小,百分比占用的空间也比它使用的要多。
    • 是的,你是对的。请检查更新的答案。
    • 设置填充就可以了。但是,为底部填充设置硬编码的负值并不理想。如果没有任何额外的性能开销,则使用编程解决方案可能是更好的选择。
    猜你喜欢
    • 2015-09-09
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    相关资源
    最近更新 更多