秒表/计时器的 Java 控制台代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3491027/
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
Java console code for StopWatch/Timer?
提问by Muhammad Maqsoodur Rehman
I have the required code for the stopwatch here. All i want is get rid of the Swing part here and display the same output in console. Can anybody help?
我这里有秒表所需的代码。我想要的只是摆脱这里的 Swing 部分并在控制台中显示相同的输出。有人可以帮忙吗?
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.text.*;
public class ElapsedTime extends JFrame
{
JLabel time;
long startTime = System.currentTimeMillis();
ElapsedTime()
{
setSize(380,200);
setTitle("http://simpleandeasycodes.blogspot.com/");
setLocation(100,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridBagLayout());
time = new JLabel("");
time.setFont(new Font("SansSerif",Font.BOLD, 36));
time.setForeground(Color.MAGENTA);
add(time);
//starting new Thread which will update time
new Thread(new Runnable()
{
public void run()
{ try
{
updateTime();
}
catch (Exception ie)
{ }
}
}).start();
}
public void updateTime()
{
try
{
while(true)
{
//geting Time in desire format
time.setText(getTimeElapsed());
//Thread sleeping for 1 sec
Thread.currentThread().sleep(1000);
}
}
catch (Exception e)
{
System.out.println("Exception in Thread Sleep : "+e);
}
}
public String getTimeElapsed()
{
long elapsedTime = System.currentTimeMillis() - startTime;
elapsedTime = elapsedTime / 1000;
String seconds = Integer.toString((int)(elapsedTime % 60));
String minutes = Integer.toString((int)((elapsedTime % 3600) / 60));
String hours = Integer.toString((int)(elapsedTime / 3600));
if (seconds.length() < 2)
seconds = "0" + seconds;
if (minutes.length() < 2)
minutes = "0" + minutes;
if (hours.length() < 2)
hours = "0" + hours;
return minutes+":"+seconds;
}
public static void main(String[] args)
{
JFrame obj = new ElapsedTime();
obj.setVisible(true);
}
}
回答by Catchwa
The keys are:
关键是:
a.) Finding which character to write to the console in order to remove the most recently-written character (\b, or \010in ASCII)
b.) Realising that you need to remember how many characters you've written to the console the last time you updated it
c.) Remembering to use printinstead of println
a.) 查找要写入控制台的字符以删除最近写入的字符(\b, 或\010ASCII 字符)
b.) 意识到您需要记住上次写入控制台的字符数更新了
c.) 记得使用print而不是println
public class Test {
public static void main(String[] args) throws InterruptedException {
int charsWritten = 0;
long start = System.currentTimeMillis();
while (1 > 0) {
Thread.sleep(1000);
long elapsedTime = System.currentTimeMillis() - start;
elapsedTime = elapsedTime / 1000;
String seconds = Integer.toString((int) (elapsedTime % 60));
String minutes = Integer.toString((int) ((elapsedTime % 3600) / 60));
String hours = Integer.toString((int) (elapsedTime / 3600));
if (seconds.length() < 2) {
seconds = "0" + seconds;
}
if (minutes.length() < 2) {
minutes = "0" + minutes;
}
if (hours.length() < 2) {
hours = "0" + hours;
}
String writeThis = hours + ":" + minutes + ":" + seconds;
for (int i = 0; i < charsWritten; i++) {
System.out.print("\b");
}
System.out.print(writeThis);
charsWritten = writeThis.length();
}
}
}
Note: you could be more efficient by only clearing the console up to only the characters you are changing but I figure you're not going to get that much of a speed improvement.
注意:您可以通过只清除控制台到您正在更改的字符来提高效率,但我认为您不会获得那么多的速度改进。
回答by Sylar
Have a look at StopWatchfrom Apache Commons. It should fulfill your needs.
看看秒表从Apache的百科全书。它应该满足您的需求。
回答by Muhammad Maqsoodur Rehman
Here's something that i have figured out myself a little while ago:
这是我不久前自己发现的一些事情:
public class DelayExample{
static int i,j;
public static void main(String[] args){
for (j= 0; j>=0; j++)
{
for (i = 0; i < 60; i++)
{
System.out.println(j+":" + i);
try
{
Thread.sleep(1000);
}catch (InterruptedException ie)
{
System.out.println(ie.getMessage());
}
}
}
}
}
Now i want the clear screen code in Java now. Also i think i have to use System.out.print() instead.
现在我想要 Java 中的清晰屏幕代码。另外我想我必须使用 System.out.print() 代替。
回答by Matschie
So there a Swing-free solution:
所以有一个无摆动的解决方案:
public class ElapsedTime{
long startTime = System.currentTimeMillis();
public ElapsedTime() {
try {
while (true) {
//Clear Console
for (int i = 0; i < 25; i++)
System.out.println();
// geting Time in desire format
System.out.println(getTimeElapsed());
// Thread sleeping for 1 sec
Thread.currentThread().sleep(1000);
}
} catch (Exception e) {
System.out.println("Exception in Thread Sleep : " + e);
}
}
public String getTimeElapsed() {
long elapsedTime = System.currentTimeMillis() - startTime;
elapsedTime = elapsedTime / 1000;
String seconds = Integer.toString((int) (elapsedTime % 60));
String minutes = Integer.toString((int) ((elapsedTime % 3600) / 60));
String hours = Integer.toString((int) (elapsedTime / 3600));
if (seconds.length() < 2)
seconds = "0" + seconds;
if (minutes.length() < 2)
minutes = "0" + minutes;
if (hours.length() < 2)
hours = "0" + hours;
return minutes + ":" + seconds;
}
public static void main(String[] args) {
new ElapsedTime();
}
}
}
I'm afraid there is no method to clear the console because Java is platform independant. I just insert 25 empty Lines so the last time disappears.
恐怕没有办法清除控制台,因为 Java 与平台无关。我只插入 25 行空行,所以最后一次消失。

