import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class DigitalClock extends JLabel {
private String pattern;
private Timer timer;
private int delay;
public DigitalClock(String pattern) {
this.pattern = pattern;
this.delay = 1000;
createTimer();
timer.start();
}
public DigitalClock(String pattern, int delay) {
this.pattern = pattern;
this.delay = delay;
createTimer();
timer.start();
}
public DigitalClock() {
pattern = "hh:mm:ss a";
this.delay = 1000;
createTimer();
timer.start();
}
private void createTimer() {
timer = new Timer(delay, new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
setText(new SimpleDateFormat(pattern).format(new Date()));
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 229, 102);
JPanel contentPane = new JPanel();
frame.setContentPane(contentPane);
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
DigitalClock digitalClock = new DigitalClock();
contentPane.add(digitalClock);
frame.setVisible(true);
}
}
參考來源 : http://tw.knowledge.yahoo.com/question/question?qid=1013061801638 , strawman
網頁
BloggerAds 廣告
標籤
- Java (96)
- Android (27)
- 演算法 (21)
- c++ (19)
- JavaScript (7)
- OpenMp (6)
- Design Pattern (4)
- 日文歌曲 (4)
- 資料結構 (4)
- Foundation Knowledge Of Programming (3)
- QUT (2)
- CodingHomeWork (1)
- Database (1)
- 英文歌詞 (1)
搜尋此網誌
2013年6月18日 星期二
數位時鐘
訂閱:
文章 (Atom)
我的網誌清單
標籤
日文歌曲
(4)
股市
(7)
股票
(9)
英文歌詞
(1)
時事
(1)
硬體(hardware)
(1)
資料結構
(4)
演算法
(21)
數學(Math)
(4)
ACM
(3)
ajax
(7)
algorithms
(1)
Android
(27)
Blog Notes(部落格記事)
(6)
C
(9)
c++
(19)
CodingHomeWork
(1)
Database
(1)
Design Pattern
(4)
Foundation Knowledge Of Programming
(3)
GWT
(1)
How
(2)
J2EE
(1)
Java
(96)
Java語言
(4)
JavaScript
(7)
Leetcode
(4)
LOL
(1)
OpenMp
(6)
QUT
(2)
Uva
(2)
Yahoo知識問答
(11)