如何解决“未报告的异常AWTException";必须被抓住或宣布被扔掉".机器人实例 [英] How to resolve "unreported exception AWTException ; must be caught or declared to be thrown". Robot instance

查看:114
本文介绍了如何解决“未报告的异常AWTException";必须被抓住或宣布被扔掉".机器人实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实例化一个类,该类包含使用Robot进行鼠标和键移动的方法时,出现错误未报告的异常AWTException;必须被捕获或声明为抛出".我尝试使用try catch在实例中,但是点击"无法通过这种方式工作,请问如何解决呢?

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
    }

推荐答案

通过使用 try-catch 之类的

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)

private void IniciarActionPerformed(java.awt.event.ActionEvent evt) throws AWTException

这篇关于如何解决“未报告的异常AWTException";必须被抓住或宣布被扔掉".机器人实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆