How to resolve “unreported exception AWTException ; must be caught or declared to be thrown”. Robot instance

*爱你&永不变心* 提交于 2019-12-11 06:05:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!