[英]updating JLabel with multithreading every second
我嘗試減少線程計數,我想在我的Jlabel中更新此計數,但是它總是顯示空標簽,這是我的代碼
public static String all;
private static int sec = 59, hour = 7, min = 59;
@Override
public void run() {
while (sec > 0) {
System.out.format("%02d:" + "%02d:" + "%02d\n", hour, min, sec);
try {
sec--;
Thread.sleep(1000L);
} catch (InterruptedException e) {
}
if (sec == 0) {
if (min == 0) {
if (hour == 0) {
break;
}
hour--;
min = 59;
}
min--;
sec = 59;
}
StringBuilder builder = new StringBuilder();
builder.append(String.valueOf(hour))
.append(":")
.append(String.valueOf(min))
.append(":")
.append(String.valueOf(sec));
all = builder.toString();
tesmy.myNum(all); //this method send the for my gui the time every second
}
}
}
我確定他發送的時間正確,但我不知道如何每秒更新標簽。我在線程中玩此類,這是我的Jlabel
JLabel jLabel = new JLabel();
jLabel.setBounds(175, 170, 70, 20);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
jLabel.setText(myString);
}
});
add(jLabel);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.