自宅では、この冬からクラスの割り当てを拡大し、実行時に GUI 要素を更新して進行状況を表示しようとして、ループを考えました。
意図された機能は、文字列を通過する際に各文字を処理することです。GUI クラスから渡された JLable は、ループの反復ごとに更新され、文字列の進行状況を示します。ただし、ループの後、最後のThread.sleep(1000)の有効期限が切れた後、ラベルに表示される内容のみが変更されます (シンセサイザーが閉じる前か後かはわかりません。関連するとは思わないでください) :/ )。
この時点で、ループが期限切れになる前に 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();
}
}