本文目錄一覽:
用java編寫一個類實現秒錶的功能
java System.currentTimeMillis() 就是獲取當前的毫秒數
開始時記錄 istart= System.currentTimeMillis();
結束時 記錄 iend= System.currentTimeMillis();
分鐘就是 Math.round((iend-istart)/(60*1000));
那 秒數 就是 Math.round((iend-istart)/1000)%60
再開啟一個定時器,定時獲取 itmp= System.currentTimeMillis();計算分鐘和秒數 顯示出來
顯示動態在跳得秒和分
java怎樣設計一個數字秒錶?
思路:
1.聲明變量:【開始時間】,【結束時間】,【總時間】。都聲明成long類型。
2.建立四個按鈕,【開始】【暫停】【繼續】【停止】
3.【開始】綁定方法:把系統當前時間賦值給【開始時間】=System.currentTimeMillis();
4.【暫停】綁定方法:把系統當前時間賦值給【結束時間】=System.currentTimeMillis();
然後【結束時間】減去【開始時間】的值賦給【總時間】並顯示出來。
5.【繼續】綁定方法:把系統當前時間賦值給【開始時間】=System.currentTimeMillis();
6.【停止】綁定方法:把系統當前時間賦值給【結束時間】=System.currentTimeMillis();
然後【結束時間】減去【開始時間】的值賦給【總時間】並顯示出來。
用JAVA寫秒錶
package test;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import java.util.Date;
import java.text.SimpleDateFormat;
/**
* pFile: StopWatch.java/p
* pDescription: /p
* pa href=””BIOZ.info/a Copyright (c) 2004/p
*
* @author a href=”mailto:chancewang78@hotmail.com”Chance/a
*/
public class StopWatch extends JFrame {
JButton btnStart,btnStop;
JLabel label;
Timer timer;
public StopWatch() {
label=new JLabel(“00:00:00.000”);
btnStart=new JButton(“start”);
btnStop=new JButton(“stop”);
final int delay=100;
final Date startTime=new Date();
final SimpleDateFormat sdf=new SimpleDateFormat(“HH:mm:ss.S”);
final Action taskPerformer = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
//顯示時間
Date d=new Date(System.currentTimeMillis()-startTime.getTime()-28800000);
label.setText(sdf.format(d));
label.repaint();
}
};
btnStart.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
startTime.setTime(new Date().getTime());
timer=new Timer(delay, taskPerformer);
timer.start();
}
});
btnStop.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt) {
if(timer!=nulltimer.isRunning())
timer.stop();
}
});
Container c=getContentPane();
c.add(label,BorderLayout.NORTH);
c.add(btnStart,BorderLayout.CENTER);
c.add(btnStop,BorderLayout.SOUTH);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
private static void createAndShowGUI() {
StopWatch window=new StopWatch();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.pack();
window.setVisible(true);
}
}
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);
}
}
}
你可以試試,希望能幫到你!
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/227822.html