import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
public class TimerTest extends JPanel implements ActionListener {
Calendar calendar1 = Calendar.getInstance();
int hour = calendar1.get(Calendar.HOUR_OF_DAY);
int min = calendar1.get(Calendar.MINUTE);
int sec = calendar1.get(Calendar.SECOND);
javax.swing.Timer timer;
JLabel lbPresent;
public TimerTest() {
timer = new javax.swing.Timer(1000, this);
timer.setInitialDelay(0);
timer.start();
lbPresent = new JLabel("현재 : " + hour + "시" + min + "분 " + sec + "초",
Label.RIGHT);
add(lbPresent);
}
public void actionPerformed(ActionEvent e) {
++sec;
Calendar calendar2 = Calendar.getInstance();
hour = calendar2.get(Calendar.HOUR_OF_DAY);
min = calendar2.get(Calendar.MINUTE);
sec = calendar2.get(Calendar.SECOND);
lbPresent.setText("현재 : " + hour + "시" + min + "분 " + sec + "초");
}
private static void createAndShowGUI() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("TimerTest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
TimerTest timerTest = new TimerTest();
timerTest.setOpaque(true);
frame.setContentPane(timerTest);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
카페 게시글
◆ 자바소스강좌
[Swing]시계 java.swing.Timer클래스 사용.
자바짱
추천 0
조회 1,368
04.10.04 09:21
댓글 2
다음검색
첫댓글 현재 시간을 알려주는 시계입니다. Timer클래스는 1초마다 이벤트를 일으켜서 알려줍니다. actionPerformed에서 시간을 보여줍니다. 시간이 증가/감소하는 예제와 이 예제를 응용하면 스탑워치'를 만들 수 있습니다. 한번 해보세요.
어떤 사이트 게시판에서 갔다가 퍼왔습니다. 작성자는 guest이더군요. ^^;