gpt4 book ai didi

java - 如何在2个定时器之间互换?在 Java 图形用户界面中

转载 作者:行者123 更新时间:2023-11-30 05:56:34 26 4
gpt4 key购买 nike

好的,基本上我正在尝试创建一个可向上和向下计数的计时器。我需要该程序在任何时间只激活一个计时器。有两个计时器,一个使变量递增,另一个使变量递减。我似乎无法正确理解,当我按下增量时,变量会增量但永远不会停止,即使我按下减量按钮也是如此。我该怎么做呢?另外,另一个简单的问题:如何返回按键方法内的值?按键默认是无效的,所以我被难住了。

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);
}

public class event implements ActionListener {

public void actionPerformed(ActionEvent e) {

if (e.getSource() == buttonAdd) {

TimeClassAdd tcAdd = new TimeClassAdd();
timer = new Timer(1000, tcAdd);
timer.start();

} else if (e.getSource() == buttonMin) {
TimeClassMin tcMin = new TimeClassMin();
timer2 = new Timer(1000, tcMin);
timer2.start();


} else if (e.getSource() == buttonReset) {
timer.stop();
timer2.stop();
// This code does not work
// Need to revert counter to 0.
}
}
}

public class TimeClassAdd implements ActionListener {

int counter = 0;

public void actionPerformed(ActionEvent f) {

String status_symbol[] = new String[4];
status_symbol[0] = "Unused";
status_symbol[1] = "Green";
status_symbol[2] = "Yellow";
status_symbol[3] = "Red";

if (counter < 3) {
counter++;
timerLabel.setText("Time left: " + status_symbol[counter]);
} else {
timerLabel.setText("Time left: " + status_symbol[counter]);
}
}
}

public class TimeClassMin implements ActionListener {

int counter = 4;

public void actionPerformed(ActionEvent d) {
String status_symbol[] = new String[4];
status_symbol[0] = "Unused";
status_symbol[1] = "Green";
status_symbol[2] = "Yellow";
status_symbol[3] = "Red";

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);
}
}

最佳答案

如果您启动第二个计时器,如果第一个计时器仍在运行,您肯定必须停止它(即在 timer2.stop() 之前调用 timer.start() ,反之亦然)。

否则两者都会干扰,即它们访问相同的字段(在本例中为 timerLabel )。根据计时,如果第二个计时器连续增加该值,则这可能看起来像。如果例如增加计时器总是在减少计时器之后不久触发,输出值将始终为 3 - Red 。计数器本身没有增加,但标签一遍又一遍地填充该值,因此看起来它完全忽略了递减的计时器。

尽管如此,如果每个计时器的计数器达到最终值,您也应该停止它。没有必要让它再运行了。

关于第二个问题:您不能分配返回值,而是修改监听器的某些字段,然后您可以在操作方法之外访问该字段。

关于java - 如何在2个定时器之间互换?在 Java 图形用户界面中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6971334/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com