Java计时器
import java.awt.FlowLayout; import java.awt.event.*; import javax.swing.*; public class TimerWin { public static void main(String[] args) { TimeWin tw = new TimeWin(); } } class TimeWin extends JFrame implements ActionListener { JTextField text; JButton bStart, bStop, bContinue; Timer time; int n = 0, start = 1; TimeWin() { time = new Timer(800, this); text = new JTextField(10); bStart = new JButton("开始计时"); bStop = new JButton("暂停计时"); bContinue = new JButton("继续计时"); bStart.addActionListener(this); bStop.addActionListener(this); bContinue.addActionListener(this); setLayout(new FlowLayout()); add(bStart); add(bStop); add(bContinue); add(text); setSize(400, 150); validate(); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public void actionPerformed(ActionEvent e) { if (e.getSource() == time) { // java.util.Date date=new java.util.Date(); // String str=date.toString().substring(11, 19); n++; text.setText("时间:" + n); } else if (e.getSource() == bStart) { time.start(); n = 0; } else if (e.getSource() == bStop) time.stop(); else if (e.getSource() == bContinue) time.restart(); } }