2016-03-15 114 views
-3

我試圖編寫程序,它將鼠標移動到某個座標(x,y)。我有這樣的錯誤:機器人不能解析爲類型

Multiple markers at this line - MoveMouse cannot be resolved

我的代碼:

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); 
    } 
} 

回答

1

MoveMouse是一個函數,而不是一類。

替換代碼在主函數

Temr temr = new Temr(); 
temr.MoveMouse(40, 30); 
+0

謝謝!我也可以讓MoveMouse像班級一樣嗎? – Brade

1
  1. MoveMouse是一個方法,所以你不應該做這樣。

  2. 如果你想從main方法調用moveMouse,你需要聲明它是靜態的。

  3. 的Java方法名典約定是:

    Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

來源: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); 
    } 
} 
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); 

    } 
} 
相關問題