我有一個帶有Dijkstra算法的ShortestPath
類和一個名爲computeRoutes
的方法。我也有一個搜索按鈕的形式 - 我想從這個按鈕調用computeRoutes
方法,但無法弄清楚如何做到這一點。Java - 在netbeans中添加一個按鈕的方法
public class ShortestPath {
public static void computeRoutes(Node source){
source.minimumDistance = 0;
PriorityQueue<Node> nodeQueue = new PriorityQueue<Node>();
nodeQueue.add(source);
while(!nodeQueue.isEmpty()){
Node u = nodeQueue.poll();
for(Edge e : u.neighbours){
Node n = e.goal;
int weight = e.weight;
int distThruU = u.minimumDistance + weight;
if(distThruU < n.minimumDistance){
nodeQueue.remove(n);
n.minimumDistance = distThruU;
n.previousNode = u;
nodeQueue.add(n);
}
}
}
}
public static List<Node> getShortestRouteTo(Node goal){
List<Node> route = new ArrayList<Node>();
for(Node node = goal; node != null; node = node.previousNode)
route.add(node);
Collections.reverse(route);
return route;
}
}
public class BPForm extends javax.swing.JFrame {
....
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {
(I want to call the computeRoutes method here)
對於一個按鈕,使用'ActionListener'。這是RoseIndia網站垃圾建議的典型代表。 – 2012-04-23 11:30:30