0

以前にこのような投稿を見たことがありますが、質問や回答が不明確なので、以前に聞いたことがある場合はご容赦ください. タイマーがあり、タイマーがオフになったときに ActionEvent が発生するようにします。javax.swing.Timer メソッドを使用したくありません。これはどのように行うことができますか?説明不要ですが参考になれば。ActionEvent.do()メソッドのようなものを探しています

私のコード:

/**
 * 
 * @param millisec time in milliseconds
 * @param ae action to occur when time is complete
 */
public BasicTimer(int millisec, ActionEvent ae){
    this.millisec = millisec;
    this.ae = ae;
}

public void start(){
    millisec += System.currentTimeMillis();
    do{
        current = System.currentTimeMillis();
    }while(current < millisec);

}

ありがとう!だんどう18

4

1 に答える 1

0

ここに、簡単なタイマーの実装があります。他のタイマーがどのように機能するかを確認しなかったのはなぜですか?

 public class AnotherTimerImpl {

        long milisecondsInterval;
        private ActionListener listener;
        private boolean shouldRun = true;

        private final Object sync = new Object();

        public AnotherTimerImpl(long interval, ActionListener listener) {
            milisecondsInterval = interval;
            this.listener = listener;
        }

        public void start() {
            setShouldRun(true);
            ExecutorService executor = Executors.newSingleThreadExecutor();
            executor.execute(new Runnable() {

                @Override
                public void run() {
                    while (isShouldRun()) {
                        listener.actionPerformed(null);
                        try {
                            Thread.sleep(milisecondsInterval);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                            break;
                        }
                    }

                }
            });
        }

        public void stop() {
            setShouldRun(false);
        }

        public boolean isShouldRun() {
            synchronized (sync) {
                return shouldRun;
            }
        }

        public void setShouldRun(boolean shouldRun) {
            synchronized (sync) {
                this.shouldRun = shouldRun;
            }
        }

    }
于 2013-12-22T08:36:11.607 に答える