0

为了这; 我希望“Continuous”按钮开始 rollthedice(),并希望“Stop”按钮停止它,但是当我再次点击“Continuous”按钮时,我希望它再次来回启动 rollthedice()

scheduler = new ScheduledThreadPoolExecutor(1);

ScheduledFuture now = null;

Runnable runner = new Runnable(){
    public void run()
    {
        rollthedice();
    }
};


if(e.getSource()==continuous)
{

    now = scheduler.scheduleAtFixedRate(runner, 0, 500, TimeUnit.MILLISECONDS);
}
if(e.getSource()==stop)
{

    now.cancel(true);

}
4

1 回答 1

1

你缺少的是这个分配给ScheduledFuture now

now = scheduler.scheduleAtFixedRate(runner, 0, 500,
                TimeUnit.MILLISECONDS);

然后你可以打电话now.cancel(true);

只需移动到类级别的声明下方,即您可以移动到您的actionPerformed方法上方。之后它工作正常。

ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1);
ScheduledFuture<?> now = null;
于 2012-10-31T19:39:31.253 回答