誰かがリセットボタンを押すまで、このタイマープログラムを無期限に実行する必要があります。このタイマープログラムは、ボタンを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);
}
}
ザック:あなたはこの質問をした人と同じ人ですか:http://stackoverflow.com/questions/6971334/how-to-interchange-between-2-timers-in-java-gui?もしそうなら、なぜ新しいユーザー名ですか? –