这个要自己做的。
java里好象没有timer类!

解决方案 »

  1.   

    自己写一个线程,让他SLEEP规定的时间不就行了……
      

  2.   

    java里有timer类的,不过,如果功能不强大的话,可以重载:)
      

  3.   

    //文件名 timerTest.java
    //javax.swing.Timer测试,实现一个进度条
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.lang.*;class controlPanel extends JPanel implements ActionListener
    {
    public controlPanel(Timer tmp)
    {
    t=tmp;

    cmdStart=new JButton("Start");
    cmdStop=new JButton("Stop");

    add(cmdStart);
    add(cmdStop);

    cmdStart.addActionListener(this);
    cmdStop.addActionListener(this);
    }

    public void actionPerformed(ActionEvent evt)
    {
    Object source=evt.getSource();
    if(source==cmdStart)
    t.start();
    else
    t.stop();
    }

    private JButton cmdStart;
    private JButton cmdStop;
    private Timer t;

    }class timerTestPanel extends JPanel implements ActionListener
    {
    public timerTestPanel()
    {
    int barLength=0;
    }

    public void actionPerformed(ActionEvent evt)
    {
    repaint();
    }

    public void paintComponent(Graphics g)
    {
    super.paintComponent(g);
    barLength++;
    g.fillRect(0,0,barLength,barWidth);
    } private final int barWidth=20; //进度条的宽度.以象素为单位.
    private int barLength; //进度条的长度,随时间的增加而增加.以象素为单位.

    }class timerTestFrame extends JFrame
    {
    public timerTestFrame()
    {
    setTitle("Timer(计时器)测试");
    setSize(500,400);
    addWindowListener(new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    });

    timerTestPanel myTimerTestPanel=new timerTestPanel();
    delay=500;
    t = new Timer(delay,myTimerTestPanel);

    controlPanel myControlPanel=new controlPanel(t);

    Container contentPane=getContentPane();
    contentPane.add(myTimerTestPanel,"Center");
    contentPane.add(myControlPanel,"South");
    }
    private Timer t;
    private int delay; //计时器的间隔时间,以毫秒为单位.
    }public class timerTest
    {
    public static void main(String[] args)
    {
    JFrame myTimerTest=new timerTestFrame();
    myTimerTest.show();
    }
    }
      

  4.   

    可以用java.util.Timer和java.util.TimerTask类来实现定时功能