实例化一个类,该类包含使用Robot进行鼠标和键移动的方法时,出现错误“未报告的异常AWTException;必须被捕获或声明为抛出”。我尝试了try catch在该实例中,但“点击”无法通过这种方式工作,请问如何解决呢?
package Ventanas;
enter code here
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
public class Sel {
Robot robot = new Robot();
public void apos() throws AWTException {
//mouseMv(1408, 1001);
//leftClick();
mouseMv(1383, 216);
leftClick();
//mouseMv(1408, 1001);
//leftClick();
}
public Sel() throws AWTException {
robot.setAutoDelay(40);
robot.setAutoWaitForIdle(true);
}
public void leftClick() throws AWTException {
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(200);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(200);
}
public void mouseMv(int x, int y) throws AWTException {
robot.mouseMove(x, y);
}
public void abrirFavoritos() throws AWTException {
//1408 999
try {
mouseMv(1408, 999);
leftClick();
} catch (NullPointerException e) {
System.out.println(e);
}
}
}
-----------------------------------------------------------------------
//Another class
private void IniciarActionPerformed(java.awt.event.ActionEvent evt) {
Metodos a = new Metodos();
Sel s = new Sel(); //Here is the error
}
答案 0 :(得分:1)
通过使用try-catch
之类的
try {
Sel s = new Sel();
// ...
} catch (AWTException ae) {
ae.printStackTrace();
}
或修改此方法的签名以引发异常。也就是说,改变
private void IniciarActionPerformed(java.awt.event.ActionEvent evt)
到
private void IniciarActionPerformed(java.awt.event.ActionEvent evt) throws AWTException