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上睡觉。我希望在声音文件播放完毕之前,我的更新不会发生。