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.
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.
Comments