2011-08-13 7 views
1

誰かがリセットボタンを押すまで、このタイマープログラムを無期限に実行する必要があります。このタイマープログラムは、ボタンを1回クリックするだけで増減します。たとえば、一度クリックすると、誰かがそれを停止または減少させるまで、増加します。問題は、私はこれについて正しいコードを作ったと思うが、それは単に実行されません。そこには論理的なエラーがあるに違いありませんが、正確にはどこを正確に突き止めるように見えません。私のコードで何が間違っているのか分かりますか?Timerプログラム内にactionlistenerを実装する方法は?

import java.awt.event.*; 
import java.awt.*; 
import javax.swing.*; 

public class TimerTutorial extends JFrame { 
    JLabel timerLabel; 
    JButton buttonAdd, buttonMin, buttonReset; 
    Timer timer; 
    Timer timer2; 

    public TimerTutorial() { 
     setLayout(new GridLayout(2, 2, 5, 5)); 
     buttonReset = new JButton("Press to reset"); 
     add(buttonReset); 

     buttonAdd = new JButton("Press to Add"); 
     add(buttonAdd); 

     buttonMin = new JButton("Press to Minus"); 
     add(buttonMin); 

     timerLabel = new JLabel("Waiting..."); 
     add(timerLabel); 

     event e = new event(); 
     buttonAdd.addActionListener(e); 
     buttonMin.addActionListener(e); 
     buttonReset.addActionListener(e); 
    } 

    public class event implements ActionListener { 
     public void actionPerformed(ActionEvent e) { 
     while (true) { 
      TimeClassAdd tcAdd = new TimeClassAdd(); 
      timer = new Timer(1000, tcAdd); 
      timer.start(); 
      timerLabel.setText("IT HAS BEGUN"); 
     } 
     } 

     public class TimeClassAdd implements ActionListener { 
     int counter = 0; 

     public void actionPerformed(ActionEvent e) { 
      String status_symbol[] = new String[4]; 
      status_symbol[0] = "Unused"; 
      status_symbol[1] = "Green"; 
      status_symbol[2] = "Yellow"; 
      status_symbol[3] = "Red"; 
      if (e.getSource() == buttonAdd) { 
       if (counter < 3) { 
        counter++; 
        timerLabel.setText("Time left: " + status_symbol[counter]); 
       } else { 
        timerLabel.setText("Time left: " + status_symbol[counter]); 
       } 
      } else if (e.getSource() == buttonMin) { 
       if (counter >= 3) { 
        counter = 3; 
        timerLabel.setText("Time left: " + status_symbol[counter]); 
        counter--; 
       } else if (counter == 2) { 
        timerLabel.setText("Time left: " + status_symbol[counter]); 
        counter--; 
       } else if (counter == 1) { 
        timerLabel.setText("Time left: " + status_symbol[counter]); 
       } 
      } 
     } 
     } 
    } 

    public static void main(String args[]) { 
     TimerTutorial gui = new TimerTutorial(); 
     gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     gui.setSize(500, 250); 
     gui.setTitle("Timer Tutorial"); 
     gui.setVisible(true); 
    } 
} 
+0

ザック:あなたはこの質問をした人と同じ人ですか:http://stackoverflow.com/questions/6971334/how-to-interchange-between-2-timers-in-java-gui?もしそうなら、なぜ新しいユーザー名ですか? –

答えて

2

このwhile (true)はスリープ状態にあなたの全体のアプリケーションを配置します:

 while (true) { 
     TimeClassAdd tcAdd = new TimeClassAdd(); 
     timer = new Timer(1000, tcAdd); 
     timer.start(); 
     timerLabel.setText("IT HAS BEGUN"); 
    } 

それがイベントを結びつけるため、単純に少なくともしないメインのSwingイベントスレッド上で、Swingアプリケーションでこれをしません他のアクションは実行できません。タイマーを使用しているので、最初はwhileループが必要ありません。

編集1
その他の問題:

  • あなたTimeClassAddタイマによって使用されるのActionListenerです。 actionPerformedメソッドに渡されるActionEventオブジェクトから返されたgetSourceは、イベントをトリガしたオブジェクトです。ここではタイマーはボタンではありません。したがって、このActionEventオブジェクトから押されたボタンを取得することはできません。
  • 代わりに、 "イベント"クラスのactionPerformedメソッドに渡されたActionEventオブジェクトから返されたgetSource()からその情報を取得する必要があります。
+0

@Zack:**編集1 **をご覧ください –

関連する問題