chenzhenhhz 发表于 2008-5-11 09:26

关于Timer的问题(里面有源码)

import java.awt.event.*;
import javax.swing.*;
class Timertest {
        public Timertest(){
                mytimeListener listener=new mytimeListener();
                Timer nowtime=new Timer(1000,listener);
                nowtime.start();
        }
        public class mytimeListener implements ActionListener{
                public void actionPerformed(ActionEvent e){ //  运行到这里就停了,提示因为没实现
//actionPerformed 方法,可是已经实现了啊,谁能帮忙解决一下,谢谢了!
                        int i=0;
                        i++;
                        System.out.println(i);
                }
        }
        public static void main(String args[]){
                new Timertest();
        }
       
}

sunkaidong 发表于 2008-5-11 09:54

import java.awt.event.*;
import javax.swing.*;
class Timertest {
    public Timertest(){
        mytimeListener listener=new mytimeListener();
        Timer nowtime=new Timer(1000,listener);
        nowtime.start();
    }
    public class mytimeListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            int i=0;
            i++;
            System.out.println(i);
        }
    }
    public static void main(String args[]){
        new Timertest();
        try{
                Thread.sleep(2000);
                }
                catch(InterruptedException E)
                {
                        System.out.println(E.toString());
                        }
    }
   
}

sunkaidong 发表于 2008-5-11 10:18

必须把主线程暂停住..要不程序谁主线程结束就结束了

chenzhenhhz 发表于 2008-5-11 10:56

回复 3# 的帖子

谢谢了,不过能不能详细点啊,我好像没定义线程啊,以前也是这么用的,好像没问题。

chenzhenhhz 发表于 2008-5-11 10:58

回复 3# 的帖子

再麻烦一下,最好能够提供一下这方面的资料,自己看看。非常感激。

sunkaidong 发表于 2008-5-11 11:02

你是没定义线程..但是timer就是个线程啊...它的作用是到时间就唤醒操作

sunkaidong 发表于 2008-5-11 11:04

import java.awt.event.*;
import javax.swing.*;
class Timertest {
    public Timertest(){
        mytimeListener listener=new mytimeListener();
        Timer nowtime=new Timer(1000,listener);
        nowtime.setRepeats(false);
        nowtime.start();
        try{
                Thread.sleep(2000);
                }
                catch(InterruptedException E)
                {
                        System.out.println(E.toString());
                        }
    }
    public class mytimeListener implements ActionListener{
        public void actionPerformed(ActionEvent e){
            int i=0;
            i++;
            System.out.println(i);
        }
    }
    public static void main(String args[]){
        new Timertest();
        
    }
   
}

chenzhenhhz 发表于 2008-5-11 11:14

回复 7# 的帖子

恩,非常感谢

页: [1]

编程论坛