java 在 ProgressBar 上绘制一个字符串,比如 JProgressBar?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14059474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:55:45  来源:igfitidea点击:

Draw a String onto a ProgressBar, like JProgressBar?

javastringprogress-barjavafxjprogressbar

提问by mattbdean

I recently started working with JavaFX, and started making FX versions of my custom Swing components. One of them was a count down timer, in which a JProgressBarwas involved. I would draw the current time onto the bar using it's setString(String)method. Unfortunately, there does not seem to be such a method with JavaFX's ProgressBar. The closest thing I saw to what I was looking for was this:

我最近开始使用 JavaFX,并开始制作自定义 Swing 组件的 FX 版本。其中之一是倒数计时器,其中JProgressBar涉及 a。我会使用它的setString(String)方法将当前时间绘制到栏上。不幸的是,JavaFX 的ProgressBar. 我看到的最接近我正在寻找的东西是这样的:

timer(source)

计时器来源

I don't know if this would require a whole new custom component, or just a class like java.awt.Graphics.

我不知道这是否需要一个全新的自定义组件,或者只是一个像java.awt.Graphics.

Any help would be much appreciated. Thanks :)

任何帮助将非常感激。谢谢 :)

回答by jewelsea

Here is a sample which (I think) does what your question is asking.

这是一个示例(我认为)可以满足您的问题。

class ProgressIndicatorBar extends StackPane {
  final private ReadOnlyDoubleProperty workDone;
  final private double totalWork;

  final private ProgressBar bar  = new ProgressBar();
  final private Text        text = new Text();
  final private String      labelFormatSpecifier;

  final private static int DEFAULT_LABEL_PADDING = 5;

  ProgressIndicatorBar(final ReadOnlyDoubleProperty workDone, final double totalWork, final String labelFormatSpecifier) {
    this.workDone  = workDone;
    this.totalWork = totalWork;
    this.labelFormatSpecifier = labelFormatSpecifier;

    syncProgress();
    workDone.addListener(new ChangeListener<Number>() {
      @Override public void changed(ObservableValue<? extends Number> observableValue, Number number, Number number2) {
        syncProgress();
      }
    });

    bar.setMaxWidth(Double.MAX_VALUE); // allows the progress bar to expand to fill available horizontal space.

    getChildren().setAll(bar, text);
  }

  // synchronizes the progress indicated with the work done.
  private void syncProgress() {
    if (workDone == null || totalWork == 0) {
      text.setText("");
      bar.setProgress(ProgressBar.INDETERMINATE_PROGRESS);
    } else {
      text.setText(String.format(labelFormatSpecifier, Math.ceil(workDone.get())));
      bar.setProgress(workDone.get() / totalWork);
    }

    bar.setMinHeight(text.getBoundsInLocal().getHeight() + DEFAULT_LABEL_PADDING * 2);
    bar.setMinWidth (text.getBoundsInLocal().getWidth()  + DEFAULT_LABEL_PADDING * 2);
  }
}

A complete executable test harnessis also available.

一个完整的可执行测试工具也可以。

Sample program output:

示例程序输出:

labeledprogressbar

标记进度条