简体   繁体   中英

Java Swing Timer Loop

imagine an array of numbers. Specific number-specific button, which has to blink. I have to go through the array. Now swing timer blinks one button ok, but if I try to put for(int I=0;i<array.length;i++) loop to go to next button - Timer does not do it. Any help would be appreciated. Thank you. Here is the code I have now:

Timer startGame = new Timer(1000, new ActionListener() {
        int colorPlay = 1;//which sqaure to blink
        int blinkingState = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            if (blinkingState < 2) {
                int i = blinkingState % 2;
                switch (i) {
                    case 0:
                        if (colorPlay == 1) {
                            greenButton.setBackground(Color.green);
                        } else if (colorPlay == 2) {
                            redButton.setBackground(Color.red);
                        } else if (colorPlay == 3) {
                            blueButton.setBackground(Color.blue);
                        } else if (colorPlay == 4) {
                            yellowButton.setBackground(Color.yellow);
                        }
                        break;
                    case 1:
                        if (colorPlay == 1) {
                            greenButton.setBackground(lightGreen);
                        } else if (colorPlay == 2) {
                            redButton.setBackground(lightRed);

                        } else if (colorPlay == 3) {
                            blueButton.setBackground(lightBlue);
                        } else if (colorPlay == 4) {
                            yellowButton.setBackground(lightYellow);
                        }
                        break;
                }//switch ends
                blinkingState++;
            }//if blinking<2 ends
        }//actionPerformed ends
    });//timer ends

Your logic seems faulty. What you want is:

  • Blink green light
  • Wait
  • Blink red light
  • Wait
  • Blink yellow light

Where a blink is

  • Set regular background colour
  • Wait
  • Set light background colour

This means you are probably better off with two Timer instances.

Timer lightTimer = new Timer( 2000, new ActionListener(){
  private int lightCounter = 0;
  @Override
  public void actionPerformed(ActionEvent e){
    switch( lightCounter ){
      case ...:
      final JButton lightButton = ...;
      lightButton.setBackground( regularColour );
      Timer blinkingTimer = new Timer( 1000, new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent e){
          lightButton.setColor( lightColour );
        }
      }
      blinkingTimer.setRepeats( false );
      blinkingTimer.start();
    }
    lightCounter++;
    if ( lightCounter == numberOfLights ){
      ((Timer)e.getSource()).stop();
    }
  }
} );
lightTimer.setRepeats( true );
lightTimer.start();

Something along the lines of the above code should do it. Note how:

  • I use a second timer to switch the blinking light back to its previous state (the BlinkingTimer variable)
  • The BlinkingTimer uses setRepeats( false ) as it only needs to be triggered once
  • The LightTimer uses setRepeats( true ) as it needs to execute multiple times, and turns itself off once it let all lights blink

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM