JAVA单行字符progressbar在NetBeans输出窗口中工作,但在CMD中不工作
我想让基于CMD字符的单行更新progressbar不仅在项目编译后可以在CMD中工作,而且在NetBeans OutputWindow中也可以工作,这里的每个人都说这根本不起作用(至少在我制作自己的progressbar之前,我在这里读了很多文章) 它在NB输出窗口中不能正常工作的真正问题是使用JAVA单行字符progressbar在NetBeans输出窗口中工作,但在CMD中不工作,java,netbeans,progress-bar,netbeans-8,Java,Netbeans,Progress Bar,Netbeans 8,我想让基于CMD字符的单行更新progressbar不仅在项目编译后可以在CMD中工作,而且在NetBeans OutputWindow中也可以工作,这里的每个人都说这根本不起作用(至少在我制作自己的progressbar之前,我在这里读了很多文章) 它在NB输出窗口中不能正常工作的真正问题是使用System.out.print()以及\r时,我发现当Thread.sleep()设置为低于250/300时,它在NB输出窗口中测试时停止响应(只有当代码停止时,它才会整体输出)但是如果我将值增加到3
System.out.print()
以及\r
时,我发现当Thread.sleep()
设置为低于250/300时,它在NB输出窗口中测试时停止响应(只有当代码停止时,它才会整体输出)但是如果我将值增加到300,那么它就可以很好地开始工作。由于我需要非常简单的无休止地运行DOS一行progressbar,只为了让用户知道正在发生什么事情,并且应用程序在运行时没有冻结,所以它非常适合我的需要
所以我就这样做了:
package jthndemo;
import java.io.PrintStream;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
public class JthnDEMO {
public static void main(final String[] args) throws InterruptedException {
SwingUtilities.invokeLater(() -> {
PrintStream out = System.out;
int pause = 300;
int progressbarLength = 15;
boolean cond = true;
String txt = "Extracting...please, wait ";
String character = "█";
String str = null;
while (cond) {
int i = 0;
str = txt;
out.print("\r" + str + character);
try {
Thread.sleep(pause);
} catch (InterruptedException ex) {
Logger.getLogger(JthnDEMO.class.getName()).log(Level.SEVERE, null, ex);
}
while (i < progressbarLength) {
str = txt;
for (int j = 0; j < i + 1; j++) {
str += character;
}
out.print("\r" + str);
try {
Thread.sleep(pause);
} catch (InterruptedException ex) {
Logger.getLogger(JthnDEMO.class.getName()).log(Level.SEVERE, null, ex);
}
i++;
}
}
});
}
}
---------------
\--------------
-|-------------
--/------------
---------------
----\----------
包jthndemo;
导入java.io.PrintStream;
导入java.util.concurrent.CompletableFuture;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入javax.swing.SwingUtilities;
公共类JthnDEMO{
公共静态void main(最终字符串[]args)引发InterruptedException{
SwingUtilities.invokeLater(()->{
PrintStream out=System.out;
int pause=300;
int progressbarLength=15;
布尔cond=true;
String txt=“正在提取……请稍候”;
字符串=”█";
字符串str=null;
while(cond){
int i=0;
str=txt;
打印输出(“\r”+str+字符);
试一试{
线程。睡眠(暂停);
}捕获(中断异常例外){
Logger.getLogger(JthnDEMO.class.getName()).log(Level.SEVERE,null,ex);
}
while(i
但令我大吃一惊的是,当我编译它并通过.bat文件从命令行运行它时,CMD窗口中的输出只进行了一个完整的循环,然后在不更新的情况下轻弹整行,我不知道为什么
有人能告诉我为什么我的progressbar代码在编译后会在NetBeans 8输出窗口中运行,而不会在Win7 x64系统CMD窗口中运行吗
注:变量cond的值将在以后的代码中更改(我将重写这一行代码,以便在代码之外的其他地方定义该变量),一旦我希望无尽的progressbar结束,就不用担心了(只说我知道)。它在我的Windows 10上工作。我更改了
字符串=”-“;
当它显示时–这可能是由于我的Windows标准字符集造成的。看到光标在开始时重新启动并一次又一次地打印相同的内容有点棘手,因此我还包括以下内容:
if (character.equals("-")) {
character = " ";
} else {
character = "-";
}
while (i < progressbarLength) { ..snip..
几秒钟后,它开始打印第一行-
(更新行,只有一行,而不是下面的3行。我只是想显示进度):
然后它不断增加-,大约每秒3个,直到有15个,看起来像这样
Extracting...please, wait ---------------
Extracting...please, wait --------------
Extracting...please, wait -------------
Extracting...please, wait ------------
Extracting...please, wait -----------
然后(由于我的改变)它开始像这样
Extracting...please, wait ---------------
Extracting...please, wait --------------
Extracting...please, wait -------------
Extracting...please, wait ------------
Extracting...please, wait -----------
当所有的-
都被覆盖时,它只是从写入-
开始
很抱歉,我的添加在您的NetBeans中不起作用。我这样做只是为了看到它确实在不断更新,而且我不喜欢-
。在所有的图形之前,我注意到一些安装使用-
-,使它看起来像是在旋转,有点像li这样做:
package jthndemo;
import java.io.PrintStream;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
public class JthnDEMO {
public static void main(final String[] args) throws InterruptedException {
SwingUtilities.invokeLater(() -> {
PrintStream out = System.out;
int pause = 300;
int progressbarLength = 15;
boolean cond = true;
String txt = "Extracting...please, wait ";
String character = "█";
String str = null;
while (cond) {
int i = 0;
str = txt;
out.print("\r" + str + character);
try {
Thread.sleep(pause);
} catch (InterruptedException ex) {
Logger.getLogger(JthnDEMO.class.getName()).log(Level.SEVERE, null, ex);
}
while (i < progressbarLength) {
str = txt;
for (int j = 0; j < i + 1; j++) {
str += character;
}
out.print("\r" + str);
try {
Thread.sleep(pause);
} catch (InterruptedException ex) {
Logger.getLogger(JthnDEMO.class.getName()).log(Level.SEVERE, null, ex);
}
i++;
}
}
});
}
}
---------------
\--------------
-|-------------
--/------------
---------------
----\----------
我认为你们的节目真的是一个很棒的等待功能。我印象深刻
(目前我的计算机上没有NetBeans、Eclipse或任何其他IDE,因此我对此并不感到困惑:)它在我的Windows 10上工作。我更改了
字符串=“-”;
当它显示时,这可能是因为我的Windows标准字符集。看到光标在开始时重新启动并一次又一次地打印相同的内容有点棘手,所以我还包括以下内容:
if (character.equals("-")) {
character = " ";
} else {
character = "-";
}
while (i < progressbarLength) { ..snip..
几秒钟后,它开始打印第一行-
(更新行,只有一行,而不是下面的3行。我只是想显示进度):
然后它不断增加-,大约每秒3个,直到有15个,看起来像这样
Extracting...please, wait ---------------
Extracting...please, wait --------------
Extracting...please, wait -------------
Extracting...please, wait ------------
Extracting...please, wait -----------
然后(由于我的改变)它开始像这样
Extracting...please, wait ---------------
Extracting...please, wait --------------
Extracting...please, wait -------------
Extracting...please, wait ------------
Extracting...please, wait -----------
当所有的-
都被覆盖时,它只是从写入-
开始
很抱歉,我的添加在您的NetBeans中不起作用。我这样做只是为了看到它确实在不断更新,而且我不喜欢-
。在所有的图形之前,我注意到一些安装使用-
-,使它看起来像是在旋转,有点像li这样做:
package jthndemo;
import java.io.PrintStream;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
public class JthnDEMO {
public static void main(final String[] args) throws InterruptedException {
SwingUtilities.invokeLater(() -> {
PrintStream out = System.out;
int pause = 300;
int progressbarLength = 15;
boolean cond = true;
String txt = "Extracting...please, wait ";
String character = "█";
String str = null;
while (cond) {
int i = 0;
str = txt;
out.print("\r" + str + character);
try {
Thread.sleep(pause);
} catch (InterruptedException ex) {
Logger.getLogger(JthnDEMO.class.getName()).log(Level.SEVERE, null, ex);
}
while (i < progressbarLength) {
str = txt;
for (int j = 0; j < i + 1; j++) {
str += character;
}
out.print("\r" + str);
try {
Thread.sleep(pause);
} catch (InterruptedException ex) {
Logger.getLogger(JthnDEMO.class.getName()).log(Level.SEVERE, null, ex);
}
i++;
}
}
});
}
}
---------------
\--------------
-|-------------
--/------------
---------------
----\----------
我认为你们的节目真的是一个很棒的等待功能。我印象深刻
(目前我的计算机上没有NetBeans、Eclipse或任何其他IDE,因此我对此并不感到困惑:)我无法重现这一点,但问题很可能是您正在使用
\r