final을 제거하면 캘린더 변수를 사용하는 동안 오류가 발생하고 최종 시간을 사용하는 동안 동적으로 변경되지 않습니다.
final Calendar cal= new GregorianCalendar();
Thread clock= new Thread(){
public void run(){
for (;;) {
int am=cal.get(Calendar.AM_PM);
int hr=cal.get(Calendar.HOUR);
int min=cal.get(Calendar.MINUTE);
int sec=cal.get(Calendar.SECOND);
if (am==0) {
jtime_Label.setText(""+hr+":"+min+":"+sec+" AM");
}else
{
jtime_Label.setText(""+hr+":"+min+":"+sec+" PM");
}
int day=cal.get(Calendar.DAY_OF_MONTH);
int mon=cal.get(Calendar.MONTH);
int year=cal.get(Calendar.YEAR);
date_label.setText(""+day+"-"+(mon+1)+"-"+year);
}
}
};
clock.start();
생성 된 현재 시간을 나타내는 GregorianCalendar 객체를 생성했습니다.
그것으로부터 시간을 읽기 위해 처리하는 동안, 당신은 그 시간을 변경하지 않습니다. 처리하는 동안 캘린더 cal의 시간을 변경해야합니다.
나는 추가하는 것이 좋습니다
cal.setTime(new Date());
또는
cal.setTimeInMillis(System.currentTimeMillis());
스레드 내부의 루프에서.
이 같은
final Calendar cal= new GregorianCalendar();
Thread clock= new Thread(){
public void run(){
for (;;) {
//// add this line
cal.setTimeInMillis(System.currentTimeMillis());
/////
int am=cal.get(Calendar.AM_PM);
int hr=cal.get(Calendar.HOUR);
int min=cal.get(Calendar.MINUTE);
int sec=cal.get(Calendar.SECOND);
if (am==0) {
jtime_Label.setText(""+hr+":"+min+":"+sec+" AM");
} else {
jtime_Label.setText(""+hr+":"+min+":"+sec+" PM");
}
int day=cal.get(Calendar.DAY_OF_MONTH);
int mon=cal.get(Calendar.MONTH);
int year=cal.get(Calendar.YEAR);
date_label.setText(""+day+"-"+(mon+1)+"-"+year);
}
}
};
clock.start();
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다