How to stop Swing timer inside the anonymous class?

Java-Newbie

Im making a guessing game but Im having a problem in Swing timer, because I cant stop it when i putted an IF statement. This is the part of my code where I encountered the problem.

continueButton.addActionListener(new  ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        f.add(firstPicblur);
        f.invalidate();
        f.remove(loadingEffectBtn);
        f.setVisible(true);
        f.repaint(); 
        Timer tt = new Timer(100, new ActionListener() {                
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                f.add(firstPiclabelA,BorderLayout.NORTH);
                f.invalidate();
                f.remove(loadingEffect);
                f.setVisible(true);
                f.repaint();
                score01.setText("Score: " + gScore);
                gScore--;                     
            }
        });
        tt.start();
        tt.setRepeats(true); 
        if(gScore == 980){
            tt.stop();

P.S this is the last problem that im solving in my Guessing Game, after this everything will be OK.

kiheru

The Timer is the source of the action event, so you can use it, if you don't want to make it a field of the class:

@Override
public void actionPerformed(ActionEvent e) {
    ...
    gscore--;
    if (gScore == 980) {
        ((Timer) e.getSource()).stop();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related