如何创建全局 Robot 变量而不引发 AWTException?

发布于 2024-08-08 19:51:54 字数 918 浏览 3 评论 0原文

我试图在 Java 类中创建一个全局 Robot 变量,而不抛出 AWTException。我能想到的唯一方法是抛出异常。我需要它是全局的原因是因为我需要在类中的其他方法中使用相同的 Robot 变量。

public class Robo{
    Robot r;

    public Robo() throws AWTException{
        r = new Robot();
    }

    public void useRobot(){       
        r.mouseMove(5, 5);
        r.toString();
    }

    public void useRobot2(){
        //r....some other things
    }
}

如果我不抛出异常,我需要在每个方法中声明一个新的机器人。

public class Robo{

    public Robo() {

    }

    public void useRobot(){
        try{
            Robot r = new Robot();
            r.mouseMove(5, 5);
            r.toString();
        }
        catch (AWTException e){}
    }

    public void useRobot2(){
        try{
            Robot r = new Robot();
            r...... //some other things
        }
        catch (AWTException e){}
    }
}

有人可以帮助我吗?

I'm trying to create a global Robot variable in a Java class without throwing an AWTException. The only way that I can come up with it is by throwing the exception. The reason I need it to be global is because I need to use the same Robot variable in other methods in the class.

public class Robo{
    Robot r;

    public Robo() throws AWTException{
        r = new Robot();
    }

    public void useRobot(){       
        r.mouseMove(5, 5);
        r.toString();
    }

    public void useRobot2(){
        //r....some other things
    }
}

If I don't throw the exception, I need to declare a new Robot in every method.

public class Robo{

    public Robo() {

    }

    public void useRobot(){
        try{
            Robot r = new Robot();
            r.mouseMove(5, 5);
            r.toString();
        }
        catch (AWTException e){}
    }

    public void useRobot2(){
        try{
            Robot r = new Robot();
            r...... //some other things
        }
        catch (AWTException e){}
    }
}

Can somebody help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

っ〆星空下的拥抱 2024-08-15 19:51:54

只需使用 throws AWTException 版本,如 java.awt.Robot 仅在 GraphicsEnvironment.isHeadless() 为 true。

这意味着您无论如何都无法使用 Robot 运行您的应用程序。

Just use the throws AWTException version, as java.awt.Robot only throws this exception when GraphicsEnvironment.isHeadless() is true.

Which means you can't run your app with Robot anyway.

浮世清欢 2024-08-15 19:51:54

是否有原因无法在构造函数中捕获 AWTException 并将其包装在 RuntimeException 中?

public Robo() {
    try {
        r = new Robot();
    } catch(AWTException e) {
        throw new RuntimeException("Failed to create java.awt.Robot for Robo instance", e);
    }
}

Is there a reason you can't catch the AWTException in the constructor and throw it wrapped inside a RuntimeException?

public Robo() {
    try {
        r = new Robot();
    } catch(AWTException e) {
        throw new RuntimeException("Failed to create java.awt.Robot for Robo instance", e);
    }
}
年少掌心 2024-08-15 19:51:54

在 Robo 类中使用静态初始化块
像这样:

public static Robot r;

static
{
    try {
        r = new Robot();
    } catch(AWTException e){e.printStrackTrace();}
}

这确保了 JVM 加载 Robo 类文件后立即初始化 Robot 类

use a static initializer block in your Robo class
like this:

public static Robot r;

static
{
    try {
        r = new Robot();
    } catch(AWTException e){e.printStrackTrace();}
}

This makes sure the Robot class is initialized as soon as the JVM loads the Robo class file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文