Search code examples
javaimageswingactionlistenerjlabel

clicking on jlabel its actionlistener is called the number of times it is clicked and more in java


sorry for my blunders..

i am creating a simple java game.in my game is a rat image(jlabel) which appears randomly on the screen we need to click on the rat to win.if i click on the screen other than that includes rat image, the number of chances should decrease.i included levels in it.each level differs by rat speed.i included timer to change speed for levels.so wen i complete 1st level it will move 2 2nd level.here i am geting difficulty.when i click on level2 button it is called 2 times and when i click on rat image the rat image's listener is called 3 times and so on.. i am not getting how to do it.....

thank u :)


Solution

  • A JLabel doesn't support an ActionListener.

    So I would guess you are using a MouseListener. If it keeps getting called one more time whenever you click on it then it sounds like you keep adding the MouseListener to the label in the event code.

    You should only have one addMouseListener() statement in your code for each component.