我尝试制作程序,将鼠标移动到某些坐标(x,y)。我有这个错误:
此行有多个标记 - 无法解析MoveMouse
我的代码:
import java.awt.AWTException;
import java.awt.Robot;
public class Temr
{
public static void main(String[] args) throws AWTException
{
MoveMouse tvoi = new MoveMouse(40, 30);
/* Multiple markers at this line
- MoveMouse cannot be resolved
to a type
- MoveMouse cannot be resolved
to a type */
}
public void MoveMouse(int a, int b) throws AWTException
{
Robot robot = new Robot();
int x;
int y;
x = a;
y = b;
robot.mouseMove(x, y);
}
}
答案 0 :(得分:1)
MoveMouse是一个函数,而不是一个类。
用
替换main函数中的代码Temr temr = new Temr();
temr.MoveMouse(40, 30);
答案 1 :(得分:1)
MoveMouse是一种方法,所以你不应该这样做。
如果您想从main方法调用moveMouse,则需要将其声明为静态。
java方法名称的代码约定是:
方法应该是动词,混合大小写,首字母小写,每个内部单词的首字母大写。
来源:http://www.oracle.com/technetwork/java/codeconventions-135099.html
我做了一些重构,这段代码可以在我的机器上运行:
import java.awt.*;
public class Test
{
public static void main(String[] args) throws AWTException
{
moveMouse(300, 300);
}
public static void moveMouse(int a, int b) throws AWTException
{
Robot robot = new Robot();
robot.mouseMove(a, b);
}
}
答案 2 :(得分:0)
MoveMouse类
public class MoveMouse {
Robot ro;
public MoveMouse(int x, int y) throws AWTException{
ro = new Robot();
ro.mouseMove(x, y);
ro.delay(1000); // 1 second delay
}
}
您可以在班级中将其称为
public class TestMove {
public static void main(String[] args) throws AWTException{
new MoveMouse(500, 500);
}
}