parent
517aada66e
commit
690d40cdb9
@ -0,0 +1,146 @@
|
||||
package wuziqiyem;
|
||||
|
||||
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 timeyzm extends JPanel {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private static final String INITIAL_LABEL_TEXT = "00:00";
|
||||
|
||||
// 计数线程
|
||||
private CountingThread thread = new CountingThread();
|
||||
|
||||
// 记录程序开始时间
|
||||
private long programStartyzm = System.currentTimeMillis();
|
||||
|
||||
// 程序一开始就是暂停的
|
||||
private long pauseStartyzm = programStartyzm;
|
||||
|
||||
// 程序暂停的总时间
|
||||
private long pauseCountyzm = 0;
|
||||
|
||||
private JLabel label = new JLabel(INITIAL_LABEL_TEXT);
|
||||
|
||||
private JButton startyzm = new JButton("开始");
|
||||
|
||||
private JButton resetyzm = new JButton("清零");
|
||||
|
||||
private JButton stopyzm = new JButton("暂停");
|
||||
|
||||
private ActionListener startyzmListener = new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
thread.stopped = false;
|
||||
programStartyzm = System.currentTimeMillis();
|
||||
pauseStartyzm = programStartyzm;
|
||||
pauseCountyzm = 0;
|
||||
startyzm.setText("开始");
|
||||
}};
|
||||
|
||||
private ActionListener stopyzmListener = new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
if (thread.stopped) {
|
||||
pauseCountyzm += (System.currentTimeMillis() - pauseStartyzm);
|
||||
thread.stopped = false;
|
||||
stopyzm.setText("暂停");
|
||||
} else {
|
||||
pauseStartyzm = System.currentTimeMillis();
|
||||
thread.stopped = true;
|
||||
stopyzm.setText("继续");
|
||||
}
|
||||
}};
|
||||
|
||||
private ActionListener resetyzmListener = new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
thread.stopped = true;
|
||||
pauseStartyzm = programStartyzm;
|
||||
pauseCountyzm = 0;
|
||||
label.setText(INITIAL_LABEL_TEXT);
|
||||
resetyzm.setText("清零");
|
||||
}
|
||||
};
|
||||
|
||||
public timeyzm(String title) {
|
||||
super();
|
||||
setLayout(new BorderLayout());
|
||||
|
||||
setupBorder();
|
||||
setupLabel();
|
||||
setupButtonsPanel();
|
||||
|
||||
startyzm.addActionListener(startyzmListener);
|
||||
stopyzm.addActionListener(stopyzmListener);
|
||||
resetyzm.addActionListener(resetyzmListener);
|
||||
thread.start(); // 计数线程一直就运行着
|
||||
}
|
||||
|
||||
// 为窗体面板添加边框
|
||||
private void setupBorder() {
|
||||
setBorder(BorderFactory.createEmptyBorder(250,50,250,50));
|
||||
}
|
||||
|
||||
// 配置按钮
|
||||
private void setupButtonsPanel() {
|
||||
JPanel buttonpanelyzm= new JPanel(new FlowLayout());
|
||||
buttonpanelyzm.add(startyzm);
|
||||
buttonpanelyzm.add(stopyzm);
|
||||
buttonpanelyzm.add(resetyzm);
|
||||
add(buttonpanelyzm, 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);
|
||||
}
|
||||
|
||||
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() - programStartyzm - pauseCountyzm;
|
||||
label.setText(format(elapsed));
|
||||
}
|
||||
try {
|
||||
sleep(1); // 1毫秒更新一次显示
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 将毫秒数格式化
|
||||
private String format(long elapsed) {
|
||||
int minute, second;
|
||||
elapsed = elapsed / 1000;
|
||||
|
||||
second = (int) (elapsed % 60);
|
||||
elapsed = elapsed / 60;
|
||||
|
||||
minute = (int) (elapsed % 60);
|
||||
|
||||
return String.format("%02d:%02d", minute, second);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in new issue