StopWatch / Timer的Java控制台代码?

时间:2010-08-16 06:26:10

标签: java

我在这里有秒表所需的代码。我想要的只是在这里摆脱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);
}
}

4 个答案:

答案 0 :(得分:3)

关键是:

a。)查找要写入控制台的字符,以便删除最近编写的字符(\b或ASCII中的\010
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();
    }
  }
}

注意:只有将控制台清理到只有你正在改变的角色才能提高效率,但我认为你不会在速度上有所提高。

答案 1 :(得分:2)

StopWatch查看Apache Commons。它应该满足你的需求。

答案 2 :(得分:0)

这是我不久前想出来的东西:

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());
            }
        }

    }
    }
} 

现在我想要Java中的清晰屏幕代码。另外我认为我必须使用System.out.print()。

答案 3 :(得分:0)

所以有一个无Swing的解决方案:

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();
}

}

我担心没有方法可以清除控制台,因为Java是独立于平台的。我只插入25个空行,所以最后一次消失。