Java 是否可以创建类似 actionPerformed() 的方法但采用不同的参数?

问题描述

我必须为一个项目创建一个非常基本的 GPS。在我的界面中,我有一个汽车从 A 点移动到 B 点的动画。使汽车移动的代码是在 actionPerformed(ActionEvent e) 方法中实现的,并且工作正常。但我不能为这个方法提供不同的参数。

我想给它的参数是界面中某些点(节点)的坐标(我使用 Dijkstra 的算法来计算最短路径)。那么有没有一种方法可以创建一个方法,该方法采用所需的参数,同时保持与 actionPerformed() 方法相同的功能?我是 Java GUI 界面的新手。

**By actionPerformed() like method 我的意思是一种在其中循环代码的方法。这就是 actionPerformed() 在我的代码中所做的。 **例如,如果我在 actionPerformed() 方法中打印某些内容,它会一直打印直到我停止代码:

//This is going to print "Example for as long as the code is running
actionPerformed(ActionEvent e){
System.out.print("Example");
}

我的实际代码:

    @Override
    public void actionPerformed(ActionEvent e) {

        autoRegulTimer++;

        auto_1.setX(auto_1.getX() + 1);  // moves the car on the x axis  

        // basic code that directs the path of the car
        if (auto_1.getX() > DijkstraMain.X1) {      
            
            if(auto_1.getY()>=DijkstraMain.Y2) {    
                auto_1.setY(DijkstraMain.Y2);       
                auto_1.setX(auto_1.getX() + 1);     
            }
            else {                                          
            auto_1.setX(DijkstraMain.X1);            
            auto_1.setY(auto_1.getY() + 1);         
            }
        }
        if (autoRegulTimer % 125 == 0) {
            if (auto_G.size() > 0) {
                auto_G.add(auto_1);
            }
        }
        repaint(); 
    }

这就是我想做的:

    public void actionPerformed(ActionEvent e,Node node) {
        //code 
    }

感谢任何帮助。谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...