我正在尝试制作一个可切换的按钮来启动和停止倒数计时器。它会启动计时器但不会停止。我有点像 java 和 swing 之类的初学者,有人能帮忙吗?这是一些代码:
private static Timer timer;
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
l1.setText(Integer.toString(timeLeft / 60) + ":" + Integer.toString(timeLeft % 60));
timeLeft--;
if (timeLeft == 0) {
boolean rest = false;
if (rest) {
timeLeft = workTime;
JOptionPane.showMessageDialog(null, "Times Up!");
rest = false;
} else {
timeLeft = restTime;
JOptionPane.showMessageDialog(null, "Times Up!");
rest = true;
}
}
}
});
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
boolean start = true;
if (start == true){
timer.start();
b1.setText("stop");
start = false;
} else if (start == false){
timer.stop(); //the part that doesn't work
b1.setText("start");
start = true;
}
}
});
这不是确切的代码,而是重要的部分
弑天下
相关分类