java計時器
㈠ java 秒錶
package demo;
import javax.swing.*;
import java.awt.HeadlessException;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Timer extends JFrame {
private static final long serialVersionUID = 1L;
private static final String INITIAL_LABEL_TEXT = "00:00:00 000";
// 計數線程
private CountingThread thread = new CountingThread();
// 記錄程序開始時間
private long programStart = System.currentTimeMillis();
// 程序一開始就是暫停的
private long pauseStart = programStart;
// 程序暫停的總時間
private long pauseCount = 0;
private JLabel label = new JLabel(INITIAL_LABEL_TEXT);
private JButton startPauseButton = new JButton("開始");
private JButton resetButton = new JButton("清零");
private ActionListener startPauseButtonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (thread.stopped) {
pauseCount += (System.currentTimeMillis() - pauseStart);
thread.stopped = false;
startPauseButton.setText("暫停");
} else {
pauseStart = System.currentTimeMillis();
thread.stopped = true;
startPauseButton.setText("繼續");
}
}
};
private ActionListener resetButtonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
pauseStart = programStart;
pauseCount = 0;
thread.stopped = true;
label.setText(INITIAL_LABEL_TEXT);
startPauseButton.setText("開始");
}
};
public Timer(String title) throws HeadlessException {
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(300, 300);
setResizable(false);
setupBorder();
setupLabel();
setupButtonsPanel();
startPauseButton.addActionListener(startPauseButtonListener);
resetButton.addActionListener(resetButtonListener);
thread.start(); // 計數線程一直就運行著
}
// 為窗體面板添加邊框
private void setupBorder() {
JPanel contentPane = new JPanel(new BorderLayout());
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
this.setContentPane(contentPane);
}
// 配置按鈕
private void setupButtonsPanel() {
JPanel panel = new JPanel(new FlowLayout());
panel.add(startPauseButton);
panel.add(resetButton);
add(panel, BorderLayout.SOUTH);
}
// 配置標簽
private void setupLabel() {
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setFont(new Font(label.getFont().getName(), label.getFont().getStyle(), 40));
this.add(label, BorderLayout.CENTER);
}
// 程序入口
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
Timer frame = new Timer("計時器");
frame.pack();
frame.setVisible(true);
}
private class CountingThread extends Thread {
public boolean stopped = true;
private CountingThread() {
setDaemon(true);
}
@Override
public void run() {
while (true) {
if (!stopped) {
long elapsed = System.currentTimeMillis() - programStart - pauseCount;
label.setText(format(elapsed));
}
try {
sleep(1); // 1毫秒更新一次顯示
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
}
}
// 將毫秒數格式化
private String format(long elapsed) {
int hour, minute, second, milli;
milli = (int) (elapsed % 1000);
elapsed = elapsed / 1000;
second = (int) (elapsed % 60);
elapsed = elapsed / 60;
minute = (int) (elapsed % 60);
elapsed = elapsed / 60;
hour = (int) (elapsed % 60);
return String.format("%02d:%02d:%02d %03d", hour, minute, second, milli);
}
}
}
你可以試試,希望能幫到你!
㈡ 用JAVA編寫計時器
計時器可以使用timer類也可以使用線程類來操作,下面是Thread做的簡單的計時器
{
publicstaticvoidmain(String[]args){
newCalculagraph().start();
}
privatelongnow=0l;
privatelongstart=System.currentTimeMillis();//程序啟動時間的毫秒值
privatelongtime;
publicvoidrun(){
while(true){
now=System.currentTimeMillis();//獲取一秒之後的毫秒值
time=now-start;//兩個時間相減的到毫秒差
System.out.format("%02d:%02d:%02d
",
time/(1000*60*60)%60/*時*/,
time/(1000*60)%60/*分*/,
time/1000%60/*秒*/);//格式化字元串輸出
try{
Thread.sleep(1000);
}catch(InterruptedExceptione){
e.printStackTrace();
}
}
}
}
㈢ java 用計時器寫一個程序,每隔一秒列印出當前時間。(不可使用循環語句,使用timer類)
new java.util.Timer().schele(new java.util.TimerTask(){
public void run(){
System.out.println(new java.util.Date());
}
}, 1000, 100);
㈣ java 中timer類的用法是什麼
現在項目中用到需要定時去檢查文件是否更新的功能。timer正好用於此處。
用法很簡單,new一個timer,然後寫一個timertask的子類即可。
package comz.autoupdatefile;
import java.util.Timer;
import java.util.TimerTask;
public class M {
public static void main(String[] args) {
// TODO todo.generated by zoer
Timer timer = new Timer();
timer.schele(new MyTask(), 1000, 2000);
}
}
class MyTask extends TimerTask {
@Override
public void run() {
System.out.println("dddd");
}
}
這樣,就可以在1秒鍾之後開始執行mytask,每兩秒鍾執行一次。
當然,timer的功能也可以通過自己構造線程,然後在線程中用sleep來模擬停止一段時間,然後再執行某個動作。
其實,看一下timertask的源碼就立即可以知道,timertask就是實現了runnable介面的。也就是說,通過timer來間隔一段時間執行一個操作,也是通過一個線程來做到的。