Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 允许在thread.sleep期间进行GUI更新_Java_Swing_Audio_Timer_Thread Sleep - Fatal编程技术网

Java 允许在thread.sleep期间进行GUI更新

Java 允许在thread.sleep期间进行GUI更新,java,swing,audio,timer,thread-sleep,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();

我有一个更新按钮的方法,如下所示:

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,并根据需要工作

使用Swing
定时器
为正确的想法打点。但你只需要一个计时器

你能做的就是将音频放入一个数据结构中,比如一个
列表
。我不知道你的音频对象是什么类型的,所以我只说
音频
。假设您有一个
音频列表
。您要做的是保留一个当前索引

private int currentIndex = 0;
然后在
计时器中
代码,您可以播放列表的第一个,然后增加
当前索引

(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
    }
}

确保您没有
设置重复(false)
。我们确实希望它重复。

简单的回答:不要在EDT上睡觉。我希望在声音文件播放完毕之前,我的更新不会发生。