java用线程实现重新开始计时的秒表的例子,求大神解答

 我来答
知道达人337
2014-10-10 · TA获得超过192个赞
知道答主
回答量:146
采纳率:0%
帮助的人:122万
展开全部
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 TimerFrame extends JFrame {

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 TimerFrame(String title) throws HeadlessException {
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocation(200, 200);
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();
}

TimerFrame frame = new TimerFrame("小小计时器");
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(17); // 使时钟显得更乱
} 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);
}
}
}
请采纳,谢谢
胆小鬼贝塔
2014-10-10 · 超过14用户采纳过TA的回答
知道答主
回答量:54
采纳率:0%
帮助的人:24.1万
展开全部
timer类就可以了 不需要多线程吧
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式