简体   繁体   中英

Java TimerTask <identifier> expected

Why does java keep giving me this error? I have read multiple ways to use timer and all have given me expected error I have been trying to get this for a while now.

     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()];
               }
          }
     }

As non-declarative statements the following

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

should be in a method. You could simply move them into the actionPerformed method:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM