Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- c#
- 프로그래머스스쿨
- thread
- Docker Desktop
- 메소드
- JavaScript
- docker
- class
- Dict
- Python
- 파이썬
- StringBuilder
- 클래스
- array
- JS
- synchronized
- 객체
- Java
- Swing
- join()
- AssertJ
- 스프링부트
- 배열
- 자바스크립트
- event
- GIT
- 저장소
- SSL
- 자바
- SpringBoot
Archives
- Today
- Total
정리노트
[자바/java] thread 디지털 시계 본문
Calendar 사용
1초마다 시간을 불러와서 label 변경
package q2;
import javax.swing.*;
import java.awt.*;
import java.util.Calendar;
public class Q2 extends JFrame {
JLabel label;
class MyThread extends Thread {
public MyThread() {
label = new JLabel("aaa");
label.setFont(new Font("Serif", Font.BOLD, 50));
label.setBounds(100, 50, 400, 50);
add(label);
}
public void run() {
for (;;) {
try {
Calendar now = Calendar.getInstance();
int h = now.get(Calendar.HOUR_OF_DAY);
int m = now.get(Calendar.MINUTE);
int s = now.get(Calendar.SECOND);
String time = h+":"+m+":"+s;
label.setText(time);
Thread.sleep(1000);
} catch(Exception e) { }
}
}
}
Q2() {
setTitle("Clock");
setSize(400, 200);
setLayout(null);
new MyThread().start();
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Q2 q = new Q2();
}
}
728x90
'프로그래밍 > Java' 카테고리의 다른 글
[자바/java] Thread, Graphics 도형을 그리고 thread로 크기변화 (0) | 2023.02.05 |
---|---|
[자바/java] Thread 활용 이미지 이동 컨트롤 (2) | 2023.02.04 |
[자바/java] thread 간 동작조정(생산자, 소비자) (0) | 2023.02.04 |
[자바/java] Thread 동기화 synchronized (0) | 2023.02.04 |
[자바/java] Thread. ThreadPool (0) | 2023.02.03 |