在thread.sleep期间允许GUI更新

时间:2014-04-11 21:41:59

标签: java swing audio timer thread-sleep

我有一个更新按钮的方法:

public void nextQuestion() {
        //get random question
        //Take level and subtract somewhere between 66 and 0 to get a question
        randomNum = (66 * level) - ((int) (Math.random() * 66));

        question.play();
        labelQue.setText(questions.get(randomNum).getQuestionText());
        while(question.getMicrosecondLength() !=  question.getMicrosecondPosition())
        {
        }

        //AnswerA
        audioA.play();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
        }
        buttonA.setText(": " + questions.get(randomNum).getA().getAnswerText());

        //AnswerB
        audioB.play();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
        }
        buttonB.setText(": " + questions.get(randomNum).getB().getAnswerText());

        //AnswerC
        audioC.play();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
        }
        buttonC.setText(": " + questions.get(randomNum).getC().getAnswerText());

        //AnswerD
        audioD.play();
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
        }
        buttonD.setText(": " + questions.get(randomNum).getD().getAnswerText());


        //Set correct to false at start of each question
        correct = false;
    }

然而,在完成所有睡眠和声音文件播放之前,它实际上并未改变我的GUI中的任何内容。我想在每次睡眠之间更新。

编辑:我尝试过使用计时器。

    //AnswerA
    Timer timer = new Timer(3000, new ActionListener(){
        @Override
        public void actionPerformed( ActionEvent e ){
            audioA.play();
            buttonA.setText(": " + questions.get(randomNum).getA().getAnswerText());
        }
    } );
    timer.setRepeats(false);
    timer.start();

    //AnswerB
    Timer timer2 = new Timer(3000, new ActionListener(){
        @Override
        public void actionPerformed( ActionEvent e ){
            audioB.play();
            buttonB.setText(": " + questions.get(randomNum).getB().getAnswerText());
        }
    } );
    timer2.setRepeats(false);
    timer2.start();

    //AnswerC
    Timer timer3 = new Timer(3000, new ActionListener(){
        @Override
        public void actionPerformed( ActionEvent e ){
            audioC.play();
            buttonC.setText(": " + questions.get(randomNum).getC().getAnswerText());
        }
    } );
    timer3.setRepeats(false);
    timer3.start();

    //AnswerD
    Timer timer4 = new Timer(3000, new ActionListener(){
        @Override
        public void actionPerformed( ActionEvent e ){
            audioD.play();
            buttonD.setText(": " + questions.get(randomNum).getD().getAnswerText());
        }
    } );
    timer4.setRepeats(false);
    timer4.start();

然而,似乎在3秒后,所有4个计时器同时熄灭。

e:更改为3k,6k,9k,12k并按需运行!

1 个答案:

答案 0 :(得分:0)

使用Swing Timer点出正确的想法。但是你只需要一个计时器。

您可以将音频放入数据结构中,例如List。我不知道您的音频对象是什么类型,所以我只会说Audio。所以说你有List<Audio> audios。你想要做的是保持当前的指数。

private int currentIndex = 0;

然后在Timer代码中,您可以播放列表中的第一个,然后递增currentIndex

(audios.get(currentIndex)).play();
currentIndex++;

对于计时器的每个滴答。您还要检查currentIndex是列表的大小,以了解计时器何时停止

public void actionPerformed(ActionEvent e) {
    if (currentIndex == audios.size()) {
        ((Timer)e.getSource()).stop();
        currentIndex = 0;  // reset for next timer start
    } else {
        // play and increment
    }
}

确保不要setRepeats(false)。我们希望它重复。