我有一个 ShortestPath
类,其中包含 Dijkstra 算法和一个名为 computeRoutes
的方法。我还有一个带有搜索按钮的表单 - 我想从此按钮调用 computeRoutes
方法,但不知道如何执行此操作。
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)
最佳答案
在 netbeans 设计器中双击此按钮。它将打开该按钮的 ActionListener 代码(如果您不知道这是什么。您应该查看按钮的事件处理)。只需使用 ShortestPath 类的对象(您已经创建了对象吗?)在这里调用computeRoutes() 即可。
关于Java - 以 netbeans 形式向按钮添加方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10279165/