我正在制作一个非常简单的应用程序以可视化排序算法,并且SwingWorker
每秒用来多次绘制数组。
如果用户按下“重置”按钮,则阵列将重新排序,现在他们可以选择再次使用哪种排序算法。
我的问题是,重置doInBackground()
后,即使实例化了new ,execute()方法也不再调用SwingWorker
。
如何使它可以execute()
根据需要拨打多次?
public void actionPerformed(ActionEvent event) {
stopSort = false;
doBubbleSort = false;
doSelectionSort = false;
doInsertionSort = false;
if (event.getSource() == bubbleButton) {
doBubbleSort = true;
sort.execute();
} else if (event.getSource() == selectionButton) {
doSelectionSort = true;
sort.execute();
} else if (event.getSource() == insertionButton) {
doInsertionSort = true;
sort.execute();
} else if (event.getSource() == resetButton) {
reset();
sort.execute();
}
}
public void reset() {
displayArr.clearSwappedIndexes();
displayArr.setFramesPainted(0);
displayArr.setComplete(false);
stopSort = true;
shuffleArr(arr);
sort = new Sorting(this, arr, displayArr);
}
如果用户按下“重置”按钮,则阵列将重新排序,现在他们可以选择再次使用哪种排序算法。
else if (event.getSource() == resetButton) {
reset();
sort.execute();
}
在我看来,就像您立即执行SwingWorker一样,并没有给用户提供选择排序算法的机会。因此,当用户单击排序按钮时,该工作人员已经被使用。
我认为代码应该是:
else if (event.getSource() == resetButton) {
reset();
//sort.execute(); // remove
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句