728x90
자바스윙을 이용한 코드
package com.test.timertest;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import javax.swing.JFrame;
import javax.swing.JLabel;
class TimerEx extends Thread{
private JLabel timerLabel; //레이블-> x최소화 최대화 있는 창
public TimerEx(JLabel timerLaber) { //메개변수로 해서 스택에 올리는 방식(요즘 많이 쓰는 방식)
this.timerLabel = timerLaber;
}
@Override
public void run() {
int n = 0;
while(true) {
timerLabel.setText(Integer.toString(n));
n++;
try {
Thread.sleep(1000); //1초마다 지연
} catch (InterruptedException e) {
return;
}
}//while문 끝
}
//JLabel timerLabel1 = new JLabel();//기존방식
}
public class TimerThread extends JFrame{
public TimerThread() {
setTitle("Thread를 상속받은 타이머 스레드 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //(JFrame.EXIT_ON_CLOSE)-> 윈도우창에 X최소화,최대화 만들어 주는창
Container c = getContentPane();
c.setLayout(new FlowLayout()); //레이아웃 생성
JLabel timerLabel = new JLabel(); //JLable-> 윈도우 맨위 창 만들어주는
timerLabel.setFont(new Font("Gothic", Font.ITALIC, 80));
c.add(timerLabel);
TimerEx th = new TimerEx(timerLabel);
setSize(300, 170); //창 크기
setVisible(true); //창을 띄워주는 여부
th.start();
}
public static void main(String[] args) {
new TimerThread();
}
}
728x90
'국비교육과정 > JAVA' 카테고리의 다른 글
JDBC 기본 설정 폼 (0) | 2022.04.25 |
---|---|
[JDBC] MariaDB-연결-Java (0) | 2022.03.17 |
★JDBCConnection 코드 (0) | 2022.03.17 |
9일차 (0) | 2022.03.17 |
★Lamda 중요코드 (0) | 2022.03.16 |
댓글