Java - 以 netbeans 形式向按钮添加方法

标签 java swing oop

我有一个 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/

相关文章:

c++ - 我可以从静态基方法获取当前类类型 ID 吗?

java - 如何使用 Spring Boot 在单个 html 页面中注入(inject)标签?

java - JFrame 将登录表单锁定在主表单前面

java - 在 JComboBox 中显示属性并注册另一个属性

java - 如何检查值是否正确?

c++ - 多态性奇怪的输出

java - 如何使用字符串的内容命名调用方法

java - Mockito 和 PowerMockito 错误

java - 如何通过向 Java 中现有的库类添加方法来创建新类?

java - 无法将组件添加到 Swing 中的 JPanel