使用线程的Java Timer

时间:2016-01-17 13:44:58

标签: java multithreading

我目前正在创建一个基本上只使用线程的计时器的GUI程序。我目前正在使用if语句编写程序。请帮忙。

 @Override
public void run() {

    while (count2 != 0) {
        try {

            Thread.sleep(1000);
             if (b == 0 && a > 0) {
                b = 60;
                a -= 1;
            }
            if (b > 59) {
                a += 1;
                b = 0;
            }
            if (c < 61 && c > 0) {
                c -= 1;
            }

            if (c > 59) {
                b += 1;
                c = 60;
                c -= 1;
            }
            if (b > 0 && b < 60 && c == 0) {
                b -= 1;
                c = 60;
            }



            jLabel4.setText("" + c);
            jLabel5.setText("" + b);
            jLabel6.setText("" + a);
            count2--;

        } catch (InterruptedException ex) {
            Logger.getLogger(Timer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    JOptionPane.showMessageDialog(this, "Time's up");

}

A是小时 B是分钟 C是秒

count2 = (a * 360) + (b * 60) + c;

谢谢:)

1 个答案:

答案 0 :(得分:0)

public static int[] splitToComponentTimes(BigDecimal biggy)
{
    long longVal = biggy.longValue();
    int hours = (int) longVal / 3600;
    int remainder = (int) longVal - hours * 3600;
    int mins = remainder / 60;
    remainder = remainder - mins * 60;
    int secs = remainder;

    int[] ints = {hours , mins , secs};
    return ints;
}

我认为这可以解决您的问题。只需将秒数放入方法中,它就会返回小时,分钟和秒钟。

原帖:Convert seconds value to hours minutes seconds?