How to interchange between 2 timers? in Java GUI

2019-07-19 18:31发布

Okay, so basically what I'm trying to create a timer that counts up and down. I need the program to activate just one timer at any one time. There are two timers, one causing a variable to increment, the other to decrement. I can't seem to get it right, when I press the increment, the variable increments but never stops, even when I press the decrement button. How do I go about doing this? Also, another quick question : How do I return a value which is within a keypress method? Keypress are by default void, so I stumped.

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

2条回答
不美不萌又怎样
2楼-- · 2019-07-19 18:51

One other problem: your reset button (or any button for that matter) won't do anything if you don't add an actionListener to it. In other words, you need to have code that looks like...

buttonReset.addActionListener(...);

somewhere in your program's code for the button to work.

查看更多
做个烂人
3楼-- · 2019-07-19 18:58

In case you start the second timer you will definitively have to stop the first one if it is still running (i.e. call timer2.stop() just before timer.start() and the other way round).

Otherwise both will interfere, i.e. they access the same fields (in this case the timerLabel). Depending on the timing this might then look like if the second timer is continuously increasing the value. If e.g. the increase timer is always triggered shortly after the decrease timer, the output value will always be 3 - Red. The counter itself is not increased but the label is filled with this value over and over again and thus looks like it is ignoring the decreasing timer completely.

Nevertheless you should also stop each timer if its counter has reached the final value. There is no need to let it run any longer.

Regarding your second question: You cannot assign a return value but instead modify some field of your listener which you can then access outside of the action method.

查看更多
登录 后发表回答