Search code examples
javatimeroverridingsuperclass

Timer doesn't feel like overriding


A friend of mine gave me this code for a timer. I must say that I'm a beginner, so I don't understand what @Override is, but the code worked for other friends.

  • tijd is an int with the value of 10
  • lx is the x value of a label
  • s1 is also an int with a value of 3

Eclipse says about the public void line:

Multiple markers at this line:

  • The method actionPerformed(ActionEvent) of type new ActionListener(){} must override a superclass method
  • implements java.awt.event.ActionListener.actionPerformed

The code is:

 final Timer t = new Timer(tijd, new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent e) {
        lx = lx-s1;
    }
});
t.start();

With it comes the suggestion of deleting @Override, but as I'm not sure of what it does I don't know if I'll be deleting an important thing of the code. I suppose it isn't there because some-one thought it would be funny to include the code.

So my questions are: 1. How do I move the label? 2. What is the problem with @Override?


Solution

  • Well, indeed the @Override is negligible in this case. Yet, the codes for moving the label is of great importance. You should base it on this:

    In the action listener of the timer do something like this: x = x - 1; And then Label.setLocation(x,y);

    That should do the trick!