Java TimerTask <identifier> 预期

标签 java timer identifier timertask

为什么java总是给我这个错误? 我已经阅读了多种使用计时器的方法,并且所有方法都给了我预期的错误 我已经尝试得到这个有一段时间了。

     private class buttonRowColumn implements ActionListener
     {
           Button cell;
           Timer timer = new Timer();
           myTask t = new MyTask();
           timer.schedule(t, 0, 500);
           public buttonRowColumn(Button cell)
           {
                this.cell = cell;
           }
           public void actionPerformed(ActionEvent e)
           {
               System.out.println("row = "+cell.getRow()+", column="+cell.getColumn());
               cell.button.setBackground(Color.red);
               while(cell.checkClicked() == false || cell.getRow() < 5)
               {
                    cell = buttons[0][cell.getColumn()];
                    if(cell != buttons[5][cell.getColumn()])
                    {
                         cell.button.setBackground(Color.red);
                          run();
                    }
                     timer.cancel();
                    cell.setClicked(true);
              }

           }
           public class MyTask extends TimerTask
           {
               public void run()
               {
                   cell.setBackground(null);
                   cell = buttons[cell.getRow() + 1][cell.getColumn()];
               }
          }
     }

最佳答案

作为非声明性陈述,以下内容

myTask t = new MyTask();
timer.schedule(t, 0, 500);

应该在一个方法中。您可以简单地将它们移动到 actionPerformed 方法中:

public void actionPerformed(ActionEvent e) {
    myTask t = new MyTask();
    timer.schedule(t, 0, 500);
    ...
}

关于Java TimerTask <identifier> 预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16594741/

相关文章:

java - JSoup 工作错误

c - 编程精确计时器,每 x 秒调用一次 c 语言函数

Java 制作类的数组列表并调用该类的方法

iphone - Xcode 4更改包标识符导致Xcode卡在“附加到”

java - jpa:repositories/@EnableJpaRepositories 与 @Repository - spring

java - Selenium 在 Firefox 和 chrome 上的点击位置

java - 更新sql java 不起作用

java - Libgdx 显示得分并每秒加 1 得分

python - While 循环要求输入并在时间到时中断

C++ 命名空间/标识符问题