我試圖做一個程序,它將幫助可視化氣泡排序算法。該腳本正確排序數組,但它不允許JFrame
打開,直到它完成。是否有辦法在繼續進行分類之前使其重新着色所有按鈕?下面發佈是目前處理排序和着色的類。泡沫分類模擬JButton着色
public class SortStart {
private JButton[] list;
private int[] randomList;
public SortStart(JButton[] list, int[] randomList){
this.list = list;
this.randomList = randomList;
}
public void run(){
String str = "";
int temp = 0;
int k = 0;
boolean swapped = true;
//Sort the colors
while(swapped){
swapped = false;
k ++;
for(int i = 0; i < randomList.length - k; i ++){
if(randomList[i] > randomList[i+1]){
temp = randomList[i];
randomList[i] = randomList[i+1];
randomList[i+1] = temp;
swapped = true;
for(int l = 0; l < randomList.length; l++){
System.out.print(randomList[l] + ", ");
}
System.out.println();
for(int j = 0; j < randomList.length; j++){
list[j].setBackground(new java.awt.Color(randomList[j],randomList[j],255));
}
}
}
}
}
}
認爲後臺線程。認爲SwingWorker。搜索這個細節。 –