Java:在定时循环期间更新GUI元素

时间:2015-06-04 00:03:23

标签: java user-interface timer synthesizer

在家里,从今年冬天开始扩展课堂作业,尝试在运行时更新GUI元素,以便在循环时显示进度。

预期的功能是,当它通过一个字符串,处理每个char时,一个从GUI类传递的JLable 在每个循环迭代中更新以显示进度串。但是,在最终 Thread.sleep(1000)到期后,我只会更改循环标签上显示的内容(我不会&#39) ;不知道在合成器关闭之前或之后,不要认为它是相关的:/)。

此时,我想了解如何获得JLabel的进步'改为"应该显示"循环到期之前的文本 - 已删除与我的问题无关的一些代码以保护有效。

import javax.sound.midi.MidiSystem;
import javax.sound.midi.Synthesizer;
import javax.sound.midi.MidiChannel;
import javax.swing.JLabel;

public class SoundPlayer {

 /*Deceleration of private variables for the class*/

/*Constructor that assighns variables and triggers doPlay()method. 
There is a reference to the JLabel 'Progress' passed in and declared beforehand, 
 so there is access.*/
}

public void doPlay() {
    int totalSound;
    playNumTimes = 1;
    defaultTime = true;
    sound = 0;
    extraSound = 0;

    try {   //Opens ability to play the music.
        Synthesizer synth = MidiSystem.getSynthesizer();
        synth.open();       //Will likely play sound, so opens channel.
        MidiChannel[] channels = synth.getChannels();

        musicNotes += ".";  //illegal character will mark end of string to play.

        //Examines each char of the String and acts accordingly.
        for (count = 0; count < musicNotes.length(); count++) { //For each char in the input.

            if (processChar(musicNotes.charAt(count))) { //If processed char is illegal and triggers a sound, play it!
                totalSound = extraSound + sound;    //Combines from given note and the < and >s.


                progress.setText("Should show");

                //PLAYS SOUND HERE!
                channels[channel].noteOn(totalSound, volume);
                Thread.sleep(300 * playNumTimes);   //Plays sound a number times equal to times.
                channels[channel].noteOff(totalSound);

                //After plays, resets values.
                playNumTimes = 1;
                defaultTime = true;
                sound = 0;      //Sound of 0 used to indicate no char has been assigned to the sound.
            }
        }
        Thread.sleep(1000);     //Wards against synthesizer from being closed prematurely and cutting off sound.
        synth.close();  //Closes after loop is finished.
    }
    catch (Exception e){
        e.printStackTrace();
    }
}

0 个答案:

没有答案