簡體   English   中英

使用線程的Java Timer

[英]Java Timer using Threading

我目前正在通過僅使用線程來創建基本上是計時器的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;

謝謝 :)

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

我認為這可以解決您的問題。 只需將秒數放在方法中,它將返回小時,分鍾和秒。

原始帖子: 將秒值轉換為小時分鍾秒?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM