import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MojTimer extends JFrame {
JButton klocek;
int y = 10;
private static MojTimer okno;
public MojTimer() {
setSize(400, 400);
setLayout(null);
klocek = new JButton();
klocek.setBounds(10, y, 20, 20);
add(klocek);
}
class Zadanie extends TimerTask {
public void run() {
y = y + 10;
klocek.setBounds(10, y, 20, 20);
}
}
public static void main(String[] args) {
okno = new MojTimer();
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
okno.setVisible(true);
Timer czas = new Timer();
MojTimer.Zadanie ruch = okno.new Zadanie(); // <<<<<<< tu jest babol
czas.schedule(ruch, 0L, 1000L);
}
}
jeżeli klasa wewnętrzna, nie statyczna, jest tworzona z innego obiektu niż ten w którym chcemy jej używać to nic dziwnego, że nie działa.