Java Swing: Change Text after delay

2020-02-01 12:31发布

Basically, I have this game where once guesses the correct answer it starts a new game with a new word. I want to display Correct! but after three seconds, change it to a empty string. How do I do that?

My attempt:

if (anagram.isCorrect(userInput.getText()))
    {

        anagram = new Anagram();
        answer.setText("CORRECT!");
        word.setText(anagram.getRandomScrambledWord());
        this.repaint();
        try
        {
        Thread.currentThread().sleep(3000);
        }
        catch (Exception e)
        {
        }
        answer.setText("");

    } else
    {
        answer.setForeground(Color.pink);
        answer.setText("INCORRECT!");
    }

Edit:

My solution:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
    {
        // TODO add your handling code here:
    if (anagram.isCorrect(userInput.getText()))
    {
        answer.setText("CORRECT!");

        ActionListener taskPerformer = new ActionListener()
    {
    public void actionPerformed(ActionEvent evt)
    {
        anagram = new Anagram();
        word.setText(anagram.getRandomScrambledWord());
        answer.setText("");
        userInput.setText("");
    }
    };
    Timer timer = new Timer(3000, taskPerformer);
    timer.setRepeats(false);
    timer.start();
    } else
    {
        answer.setForeground(Color.pink);
        answer.setText("INCORRECT!");
    }
    }

I am not sure, but I hope that I am following MadProgrammer's advice and not blocking the event itself, but the new thread. I will look up Java Timer also.

3条回答
可以哭但决不认输i
2楼-- · 2020-02-01 12:47

if these piece of code is in the event handlers, then you are holding up the UI thread, and it is not going to work as UI update will only happens after you finished your work in the event handlers.

You should create another thread do the work of "sleep 3 second, and change the text field, and trigger repaint". Using Timer or similar utilities is the easiest way to achieve what I am describing.

查看更多
唯我独甜
3楼-- · 2020-02-01 12:58

Swing is an event driven environment. While you block the Event Dispatching Thread, no new events can be processed.

You should never block the EDT with any time consuming process (such as I/O, loops or Thread#sleep for example).

You might like to have a read through The Event Dispatch Thread for more information.

Instead, you should use a javax.swing.Timer. It will trigger a ActionListener after a given delay.

The benefit of which is that the actionPerformed method is executed with the context of the Event Dispatching Thread.

Check out this or this or this or this for an examples

查看更多
一夜七次
4楼-- · 2020-02-01 13:10

it works after 3 seconds..

ActionListener taskPerformer = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {
            statusbar.setText("Status");
        }
    };
    Timer timer = new Timer(3000, taskPerformer);
    timer.setRepeats(false);
    timer.start();
查看更多
登录 后发表回答