progressbar java,ProgressBar.java

本文介绍了一个Java实现的ProgressBar类,详细讲解了构造方法、参数设置(如最大值、进度长度、显示模式等)以及redraw方法的工作原理。通过实例展示了如何创建、更新和隐藏进度条,并提供了设置格式选项。
摘要由CSDN通过智能技术生成

public class ProgressBar {

private long maximumValue = 0x64L;

private long barLength = 0x32L;

private char displayPattern = '\u003d';

private char blankPattern = '\u0020';

private char indicator = '\u003e';

private String percentageFormat = "%.1f%%";

private long value = 0x0L;

public ProgressBar() {

super();

}

/**

* @param maximumValue

*/

public ProgressBar(long maximumValue) {

super();

setMaximumValue(maximumValue);

}

/**

* @param maximumValue

* @param barLength

*/

public ProgressBar(long maximumValue, long barLength) {

super();

setMaximumValue(maximumValue);

setBarLength(barLength);

}

public void redraw() {

if (value > maximumValue) {

return;

}

long pBarLength = barLength - 9;

long currentBarLength = 0;

if (value == 0) {

currentBarLength = 0;

} else if (value == maximumValue) {

currentBarLength = pBarLength;

} else {

currentBarLength = value * pBarLength / maximumValue;

}

String pasty = "";

for (int i = 0; i < pBarLength; i++) {

if (i == currentBarLength - 1 && value != maximumValue) {

pasty += indicator;

continue;

}

if (i < currentBarLength) {

pasty += displayPattern;

continue;

}

pasty += blankPattern;

}

System.out.printf("\r\u005b%-" + pBarLength + "s\u005d\u0020" + percentageFormat

+ "\u0020\u0020", pasty, (float) (value * 1.0 / maximumValue * 100));

}

/**

* HIDE THE PROGRESS BAR

*/

public void hide() {

String pasty = "";

for (int i = 0; i < barLength; i++) {

pasty += "\u0020";

}

System.out.printf("\r" + pasty);

}

public void end() {

end(false);

}

/**

* @param hide

*/

public void end(boolean hide) {

if (hide) {

hide();

}

System.out.printf("\r\n");

}

/**

* @return the maximumValue

*/

public long getMaximumValue() {

return maximumValue;

}

/**

* @param maximumValue

*            the maximumValue to set

*/

public void setMaximumValue(long maximumValue) {

this.maximumValue = maximumValue;

}

/**

* @return the barLength

*/

public long getBarLength() {

return barLength;

}

/**

* @param barLength

*            the barLength to set

*/

public void setBarLength(long barLength) {

this.barLength = barLength;

}

/**

* @return the displayPattern

*/

public char getDisplayPattern() {

return displayPattern;

}

/**

* @param displayPattern

*            the displayPattern to set

*/

public void setDisplayPattern(char displayPattern) {

this.displayPattern = displayPattern;

}

/**

* @return the blankPattern

*/

public char getBlankPattern() {

return blankPattern;

}

/**

* @param blankPattern

*            the blankPattern to set

*/

public void setBlankPattern(char blankPattern) {

this.blankPattern = blankPattern;

}

/**

* @return the indicator

*/

public char getIndicator() {

return indicator;

}

/**

* @param indicator

*            the indicator to set

*/

public void setIndicator(char indicator) {

this.indicator = indicator;

}

/**

* @return the percentageFormat

*/

public String getPercentageFormat() {

return percentageFormat;

}

/**

* @param percentageFormat

*            the percentageFormat to set

*/

public void setPercentageFormat(String percentageFormat) {

this.percentageFormat = percentageFormat;

}

/**

* @return the value

*/

public long getValue() {

return value;

}

/**

* @param value

*            the value to set

*/

public void setValue(long value) {

this.value = value;

redraw();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值