1

所以,我正在修补 Java 中的机器人类。我是一个非常新的 Java 程序员,但我对其他语言有更深的根基。这是我的代码:

import java.awt.*;

public class Main {

    public static void main(String[] args) {
        Robot bot = new Robot();
        bot.mouseMove(50, 50);  
    }
}

我所做的只是看看我是否可以控制鼠标,例如将鼠标移动到 50、50。但是,在 Eclipse 中,它会在旁边放置一个红色 X

Robot bot = new Robot();

..说..

Unhandled exception type AWTException

并且不会让我运行它。谁能帮我弄清楚为什么会这样?

4

2 回答 2

6

您需要尝试/捕获异常:

import java.awt.*;
public class Main{

public static void main(String[] args) {

    try
    {
    Robot bot = new Robot();
    bot.mouseMove(50, 50);  
    }
    catch (AWTException e)
    {
    e.printStackTrace();
    }
}
}

或抛出异常:

import java.awt.*;
public class Main throws AWTException{

public static void main(String[] args) {
    Robot bot = new Robot();
    bot.mouseMove(50, 50);  
}
}
于 2012-12-09T08:20:58.170 回答
2
import java.awt.*;

public class remote{

     public static void main(String[] args) {


         try
            {
            Robot bot = new Robot();
            bot.mouseMove(50, 50);
            trace("działam");
            }
            catch (AWTException e)
            {
            e.printStackTrace();
            }
        }

     public static void trace(String s){
         System.out.print(s.toString());
     }

}

在我将 bot.mouseMove(50, 50) 添加到 TRY 指令之前,这对我有用。

于 2013-08-20T12:18:38.610 回答