您应该在单独的线程中进行处理,如 MДΓΓ БДLL 建议的那样。基本上,您必须在一个实现的类中实现与搜索相关的代码,该类Runnable
“标记”一个类可以在线程中执行。
为此,您可以使用SwingWorker
:
SwingWorker<Integer[], Void> worker = new SwingWorker<Integer[], Void>() {
public Integer[] doInBackground() {
//do the computation here. This will be executed in a different thread;
//thus allowing the event dispatch thread (=GUI thread) to ensure responsiveness of the UI.
//NEVER update your GUI here since this could cause strange errors that are sometimes hard to track down.
}
public void done() {
try {
Integer[] result = get(); //this is executed in the GUI thread after
//execution if the doInBackground method finished and fetches the result of
//that method. You should update your GUI here.
} catch (InterruptedException ex) {
ex.printStackTrace();
} catch (ExecutionException ex) {
ex.printStackTrace();
}
}
}
对于您的第二个答案:以可用于不同数据类型的通用方式实现算法相当困难,特别是因为您使用 BFS、DFS 和 A-Star 的树可能包含任何类型的数据。我认为您应该在教科书或讲座节点中找到伪代码中的算法;如果没有,请在某个地方查找并尝试自己实现它。