简体   繁体   中英

How to get this project to pause for the given amount of time after the button is pressed

So, I am trying to figure out how to add pauses in my card game to make the CPU act like they are taking turns. But, there's too much code involved, so I think if we demonstrate the strategy on this code, it could work.

How can I make this code pause for, say 5 seconds after the button has been pushed and THEN print the message.

public class TimerTest extends JFrame implements ActionListener{

private static final long serialVersionUID = 7416567620110237028L;
JTextArea area;
Timer timer;
int count; // Counts the number of sendings done by the timer
boolean running; // Indicates if the timer is started (true) or stopped (false)

public TimerTest() {
    super("Test");
    setBounds(30,30,500,500);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setLayout(null);

    area = new JTextArea();
    area.setBounds(0, 0, 500, 400);
    add(area);

    JButton button = new JButton("Click Me!");
    button.addActionListener(this);
    button.setBounds(200, 400, 100, 40);
    add(button);

    // Initialization of the timer. 1 second delay and this class as ActionListener
    timer = new Timer(5000, this);
    timer.setRepeats(true); // Send events until someone stops it
    count = 0; // in the beginning, 0 events sended by timer
    running = false;
    System.out.println(timer.isRepeats());
    setVisible(true); // Shows the frame
}

public void actionPerformed(ActionEvent e) {
    if (! running) {
        timer.start();
        running = true;
    }
    // Writing the current time and increasing the cont times
    area.append(Calendar.getInstance().getTime().toString()+"\n");
    count++;
    if (count == 10) {
        timer.stop();
        count = 0;
        running = false;
    }
}

public static void main(String[] args) {
    // Executing the frame with its Timer
    new TimerTest();
}
}

What I really would like to do is to be able to insert a pause into my method that runs the actions of each CPU. But it sounds like, with the Timer class, you need to perform your post-pause actions in the Timer's action listener. I am mostly confused about why the Timer needs an actionListener. I don't need the timer to repeat itself after it is done.

If it would be better for me to post my own code, let me know. But I am not sure what parts would be useful, since I don't want to have tons of code in this thread.

Conceptually, you want to trigger some event after 5 seconds, Swing Timer is perfect for this AND it's thread safe, making it safe to update the UI from, without the need for more hoop jumping.

The Timer sets up it's own thread to wait in, so it won't block the EDT, this means that you need to wait till the timer is triggered before you can something, it won't block and wait where you call it, this is the reason it has an ActionListener , so you know when it's triggered.

You can use your existing Timer , assuming it's not doing anything else, but for argument sake, I'm creating a new one...

private Timer waitItOut;
private JButton button;

Then in your constructor, you set up the timer, the difference here is I've made it non-repeating, meaning it won't trigger every 5 seconds, but you can re-run it when you want...

waitItOut = new Timer(5000, new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent evt) {
        doImportantWork();
    }
});
waitItOut.setRepeats(false);

// I made this an instance field for demonstration purposes
button = new JButton("Click Me!");
button.addActionListener(this);
button.setBounds(200, 400, 100, 40);
add(button);

Then in your actionPerformed method, you simply start the timer...

public void actionPerformed(ActionEvent e) {
    button.setEnabled(false);
    waitItOut.restart();
}

And wait till it calls your "important method which should be run after the specified delay"...

public void doImportantWork() {
    button.setEnabled(false);
    // Writing the current time and increasing the cont times
    area.append(Calendar.getInstance().getTime().toString()+"\n");
    count++;
    if (count == 10) {
        count = 0;
        running = false;
    }
}

You should launch a new thread which does the following:

  • Sleep for 5 seconds
  • Sets your required message
  • Calls repaint() to signal that the message needs to be redrawn.

On a action performed situation, you can initiate certain functions like: TimeUnit.SECONDS.sleep(1); to do this. I see in your code that you are trying to manually count for time. You can let the compiler or cpu do this for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM