2012-04-23 30 views
0

我有一個帶有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) 

回答

3

在NetBeans Designer中雙擊此按鈕。它會打開ActionListener的代碼(如果你不知道這是什麼,你應該看看按鈕的事件處理)。只需使用ShortestPath Class的Object(你已經創建了一個對象?)在這裏調用computeRoutes()。

2

我想你已經實現的ActionListener,並在你的代碼必須覆蓋

public void actionPerformed(ActionEvent e) { 
    if (e.getSource() == computeRoutes) { 
     // put the logic here 
    } 
..... 
} 
相關問題