为什么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()];
}
}
}发布于 2013-05-17 02:29:06
作为非声明语句,如下所示
myTask t = new MyTask();
timer.schedule(t, 0, 500);应该在一个方法中。您可以简单地将它们移到actionPerformed方法中:
public void actionPerformed(ActionEvent e) {
myTask t = new MyTask();
timer.schedule(t, 0, 500);
...
}https://stackoverflow.com/questions/16594741
复制相似问题