2016-03-23 29 views
1

これは私のコードに含まれていて、ActionListenerにシンボルが見つからないということが続くので、動作しません。私はそれを動作させる方法を知らない。しかし、これは動作しません画像付きタイマーとJSlider

private static JLabel value; 
private static ImageIcon image; 
private static Timer timer; 
private static final int delay = 2000; 
private static int newDelay; 
private static int i = 1; 

    timer = new Timer(delay, new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
     // makes the image at i appear and then goes to 2 and so on until i i = 8 and will return a 1 after. Will keep on doing so         
      value.setIcon(new ImageIcon(i + ".png")); 
      i++; 

      if(i == 8) { 
      i = 1; 
      } 
     } 
    }); 
    timer.start(); 


} 


private static class SliderChange implements ChangeListener { 

    public void stateChanged(ChangeEvent event) { 

     JSlider source = (JSlider) event.getSource(); 
     // while it is adjusting timer stops and gets the value of where the slider hits and the newDelay will be the new timer time. (So if they drag slider to 6, delay(which is 2000) will be divided by 6 to get new time 
     if (!source.getValueIsAdjusting()) { 
      timer.stop(); 
      value.setIcon(new ImageIcon(i + ".png")); 
      newDelay = (delay/(int)source.getValue()); 
      timer = new Timer(newDelay, new Actionlistener()); 
      timer.start(); 
     } 

    } 

} 

だから基本的に私が何をしようとしているスライダーの土地とは、どのようにIDK場所に応じて1-8.png動きからイメージを作成します。どうすれば修正できますか?

それはエラーがあると言って、この行を指す:

timer = new Timer(newDelay, new Actionlistener()); 
+1

を混乱さinterfaceの要件を実装することなく、interfaceのインスタンスを作成しようとしています毎回新しい「Timer」を作成する必要があるかどうかわからない場合は、現在のタイマーを停止して、単純にそれを変更して再起動してください。問題は、 'new ActionListener())'が、 'interface'の要件を実装せずに' interface'のインスタンスを作成しようとしていることです – MadProgrammer

答えて

1

私はそれがTimerたびを再作成するために本当に必要ですか分かりません。代わりに、問題がtimer = new Timer(newDelay, new Actionlistener());で、それを止めることがdelayプロパティの設定と

private static class SliderChange implements ChangeListener { 

    public void stateChanged(ChangeEvent event) { 

     JSlider source = (JSlider) event.getSource(); 
     // while it is adjusting timer stops and gets the value of where the slider hits and the newDelay will be the new timer time. (So if they drag slider to 6, delay(which is 2000) will be divided by 6 to get new time 
     if (!source.getValueIsAdjusting()) { 
      timer.stop(); 
      value.setIcon(new ImageIcon(i + ".png")); 
      newDelay = (delay/(int)source.getValue()); 
      timer.setDelay(newDelay); 
      timer.start(); 
     } 

    } 

} 

代わり

それを再起動するには、コンパイラに