问题
I have the error "unreported exception AWTException ; must be caught or declared to be thrown" instantiating a class that contain methods with mouse and key movements using Robot. I tried with try catch In the instance but the "click" doesnt work this way, what is the problem how to solve it?
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
}
回答1:
By using a try-catch
like
try {
Sel s = new Sel();
// ...
} catch (AWTException ae) {
ae.printStackTrace();
}
Or modifying the signature of this method to also throw the exception. That is, change
private void IniciarActionPerformed(java.awt.event.ActionEvent evt)
to
private void IniciarActionPerformed(java.awt.event.ActionEvent evt) throws AWTException
来源:https://stackoverflow.com/questions/54084517/how-to-resolve-unreported-exception-awtexception-must-be-caught-or-declared-t