如题,想要一段计时代码,并且在lable显示出来;有两个按钮,一个开始,一个结束。
点开始,lable就开始计时。要求计时格式为:HH:MM:SS如图:

解决方案 »

  1.   

    1、创建Timer,设置时间间隔为1000ms2、使用SimpleDateFormat对当前时间进行格式化,取得Date的String3、使用Label.setText()
      

  2.   

    以前做的
    你参考下吧
    还是自己动手效果好import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class TimerTest
    {
    public static void main(String[] args)
    {
    MFrame frame = new MFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    }
    }class MFrame extends JFrame
    {
    private static final int width = 300;
    private static final int height = 160;
    private JLabel label;
    private Time time;
    private Timer t;

    public MFrame()
    {
    setTitle("TIMER");
    setBounds(200, 200, width, height);

    time = new Time();

    ActionListener listener = new 
    ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    time.timePass();
    label.setText(time.getTime());
    }
    };
    t = new Timer(10, listener);

    Container con = getContentPane();
    label = new JLabel(time.getTime(), JLabel.CENTER);
    label.setFont(new Font("仿宋", Font.BOLD, 25));
    con.add(label, BorderLayout.CENTER);
    MPanel panel = new MPanel();
    con.add(panel, BorderLayout.SOUTH);
    }

    class MPanel extends JPanel
    {
    public MPanel()
    {
    JButton start = new JButton("Start");
    start.addActionListener(new
    ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    t.start();
    }
    });
    add(start);

    JButton stop = new JButton("Stop");
    stop.addActionListener(new
    ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    t.stop();
    }
    });
    add(stop);

    JButton reset = new JButton("Reset");
    reset.addActionListener(new
    ActionListener()
    {
    public void actionPerformed(ActionEvent e)
    {
    time.reset();
    label.setText(time.getTime());
    }
    });
    add(reset);
    }
    }
    }class Time
    {
    private int hour, min, sec, tmsec;

    public Time()
    {
    hour = 0;
    min = 0;
    sec = 0;
    tmsec = 0;
    }

    public void timePass()
    {
    tmsec++;
    if(tmsec == 100)
    {
    tmsec = 0;
    sec++;
    if(sec == 60)
    {
    sec = 0;
    min++;
    if(min == 60)
    {
    min = 0;
    hour++;
    if(hour == 24)
    {
    hour = 0;
    }
    }
    }
    }
    }

    public String getTime()
    {
    return String.format("%1$02d : %2$02d : %3$02d : %4$02d", hour, min, sec, tmsec);
    }

    public void reset()
    {
    hour = 0;
    min = 0;
    sec = 0;
    tmsec = 0;
    }
    }
      

  3.   

    long startTime = System.currentTimeMillis();//以毫秒返回系统时间
    long passedTime;
    passedTime=System.currentTimeMillis()-startTime;
    strPass=simpleDateFormat.format(passedTime);//将passedTime转换为mm:ss:sss格式
    label.setText("用时:"+strPass);
      

  4.   

    用Thread 来控制下
      sleep(1000);
    SimpleFormatData sf = new SimpleFormatDate("YYYY-MM-dd:hh:mm:ss");
    Date d = sf.format(new Date());
     label.setText(d+"");