日期:2014-05-20  浏览次数:20684 次

javax.swing.Timer类的问题
代码如下:
Timer timer = new Timer(1000,new TimerListener());
timer.setDelay(20000);


我在重新设置完了延迟时间20000后为什么这个timer对象产生事件的时间间隔还是1000ms啊,在设置完20000ms这个新的延迟时间后,我用timer.getDelay()查看新的延迟时间的确是20000,那为什么这个timer产生事件的时间间隔还是1000ms啊。


------解决方案--------------------
构造函数中的参数才是延时时间
------解决方案--------------------
探讨
那我用timer.setDelay(20000);设置的不也是延时时间吗

------解决方案--------------------
为什么我不会出现这种情况呢?!
Java code

public class TimerTest extends JFrame {
    public static void main(String[] args) {
        new TimerTest();
    }

    public TimerTest() {
        this.setBounds(300, 300, 300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
        Timer timer = new Timer(1000, new TestActionListener());
        timer.setDelay(2000);
        timer.start();
        System.out.println("start : " + System.currentTimeMillis());
    }
}

class TestActionListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
        long now = System.currentTimeMillis();
        System.out.println(now);
    }
}

------解决方案--------------------
setDelay确实是延时,
不过调整首次延时需要用setInitialDelay
------解决方案--------------------
我自己编译了一下怎么可以啊?用setDelay是可以的啊~还有楼主这个简单的程序为什么还要弄个对话框啊~