简体   繁体   English

在JProgressBar上显示自定义进度字符串

[英]Displaying a custom progress string on a JProgressBar

When setStringPainted() of JProgressBar is used,the amount of process completion is displayed in terms of percentage. 当使用JProgressBar setStringPainted() ,将以百分比显示过程完成的数量。

But how can i customise setStringPainted() so that i can display remaining time instead of percentage? 但是,如何自定义setStringPainted()以便可以显示剩余时间而不是百分比?

setString() sets the progress string. setString()设置进度字符串。 If that property is null then only a simple percentage is shown. 如果该属性为null则仅显示一个简单的百分比。 This is pointed out clearly in the documentation: 文档中明确指出了这一点:

setString 的SetString

public void setString(String s)

Sets the value of the progress string. 设置进度字符串的值。 By default, this string is null , implying the built-in behavior of using a simple percent string. 默认情况下,此字符串为null ,表示使用简单百分比字符串的内置行为。 If you have provided a custom progress string and want to revert to the built-in behavior, set the string back to null . 如果您提供了一个自定义进度字符串,并且想要恢复为内置行为,请将字符串设置回null

The progress string is painted only if the isStringPainted method returns true. 仅当isStringPainted方法返回true时,才会绘制进度字符串。

Parameters: 参数:

s – the value of the progress string s –进度字符串的值

Interestingly (read: I'm astonished :-) you'll have to implement any value-dependent progress string yourself, by overriding getString 有趣的是(您读过:我很惊讶:-),您将不得不通过重写getString来实现任何与值相关的进度字符串。

    final JProgressBar bar = new JProgressBar() {

        @Override
        public String getString() {
            int max = getMaximum();
            return super.getString() + (max - getValue());
        }

    };
    bar.setStringPainted(true);
    ActionListener l = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            bar.setValue(bar.getValue() + 1);
        }
    };
    bar.setString("missing: ");
    new Timer(500, l).start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM