2014-04-25 2 views
0

그래서 나는 만든 게임 ive를위한 타이머 클래스를 만들었고 이미 시작 버튼 동작을 타이머를 시작하도록 설정했지만 타이머를 멈추고 다시 0으로 재설정하는 방법에 대해서는 약간 혼란 스럽습니다. 누구든지 내 문제에 대한 해결책을 제시 할 수 있다면 좋겠다.어떻게 타이머를 멈 춥니 까?

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

public class MyTimer extends Panel { 


private JLabel timeDisplay; 
private JButton resetButton; 
private JButton startButton; 
private JButton stopButton; 
Timer timer; 

public MyTimer(){ 

    MyTimer timer; 
    startButton = new JButton("Start Timer"); 
    stopButton = new JButton("Stop Timer"); 
    timeDisplay = new JLabel("...Waiting..."); 
    resetButton = new JButton("Reset Timer"); 

    this.add(resetButton); 
    this.add(startButton); 
    this.add(stopButton); 
    this.add(timeDisplay); 

    event e = new event(); 
    startButton.addActionListener(e); 

    event1 c = new event1(); 
    stopButton.addActionListener(c); 

    event2 d = new event2(); 
    resetButton.addActionListener(d); 

} 

public class event implements ActionListener{ 
    public void actionPerformed(ActionEvent e){ 
     int count = 0; 
     timeDisplay.setText("Elapsed Time in Seconds: " + count); 

     TimeClass tc = new TimeClass(count); 
     timer = new Timer(1000, tc); 
     timer.start(); 
    } 
} 

public class TimeClass implements ActionListener{ 
    int counter; 

    public TimeClass(int counter){ 

     this.counter = counter; 

    } 

    public void actionPerformed(ActionEvent e){ 
     counter++; 

     timeDisplay.setText("Elapsed Time in Seconds: " + counter); 

    } 
} 

class event1 implements ActionListener{ 
    public void actionPerformed (ActionEvent c){ 
     timer.stop(); 
    } 
} 

class event2 implements ActionListener{ 
    public void actionPerformed (ActionEvent d){ 
     timer.restart(); 
    } 
} 
} 

EDIT 내가 정지 버튼이 완벽 할 필요가 일을 가지고 있지만, 리셋 버튼에 문제가 있으므로

좋아. 예를 들어 일시 중지 된 지점부터 다시 시작하기 전에 잠시 타이머를 일시 중지합니다 (예 : 초당 4 초로 일시 중지 한 후 5, 6 등으로 계속 진행).

제안 사항이 있습니까?

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

public class MyTimer extends Panel { 


    private JLabel timeDisplay; 
    private JButton resetButton; 
    private JButton startButton; 
    private JButton stopButton; 
    Timer timer; 

    public MyTimer(){ 

     MyTimer timer; 
     startButton = new JButton("Start Timer"); 
     stopButton = new JButton("Stop Timer"); 
     timeDisplay = new JLabel("...Waiting..."); 
     resetButton = new JButton("Reset Timer"); 

     this.add(resetButton); 
     this.add(startButton); 
     this.add(stopButton); 
     this.add(timeDisplay); 

     event e = new event(); 
     startButton.addActionListener(e); 

     event1 c = new event1(); 
     stopButton.addActionListener(c); 

     event2 d = new event2(); 
     resetButton.addActionListener(d); 

    } 

    public class event implements ActionListener{ 
     public void actionPerformed(ActionEvent e){ 
      int count = 0; 
      timeDisplay.setText("Elapsed Time in Seconds: " + count); 

      TimeClass tc = new TimeClass(count); 
      timer = new Timer(1000, tc); 
      timer.start(); 
     } 
    } 

    public class TimeClass implements ActionListener{ 
     int counter; 

     public TimeClass(int counter){ 

      this.counter = counter; 

     } 

     public void actionPerformed(ActionEvent e){ 
      counter++; 

      timeDisplay.setText("Elapsed Time in Seconds: " + counter); 

     } 
    } 

    class event1 implements ActionListener{ 
     public void actionPerformed (ActionEvent c){ 
      timer.stop(); 
     } 
    } 

    class event2 implements ActionListener{ 
     public void actionPerformed (ActionEvent d){ 
      timer.restart(); 
     } 
    } 
} 
+2

'timer.stop()'을 사용하십시오. – Braj

답변

0

로 전화하십시오. restart() 메서드를 사용하여 다시 시작하십시오. .stop()을 사용하여 중지하십시오.

0

How to Use Swing Timers에서 샘플 코드를 살펴보십시오.

timer.restart()을 사용하여 다시 시작하고 timer.stop()을 사용하여 중지하십시오.

관련 문제