简体   繁体   English

Java Robot - 类函数问题

[英]Java Robot - Class functions issue

I am trying to make a separate class for the Java AWT Robot to use with projects but i am having trouble setting it up how i would like as all of the examples I have found online seem to pack the code into a single .java file instead. 我正在尝试为Java AWT Robot创建一个单独的类来与项目一起使用,但我无法设置它我想要的方式,因为我在网上找到的所有示例似乎都将代码打包到单个.java文件中。

My code works fine however I am wondering if I could setup the functions in a nicer way. 我的代码工作正常,但我想知道我是否可以更好地设置功能。

The code for the RobotLib.java class is as follows: RobotLib.java类的代码如下:

package com.z;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

    public class RobotLib {

    private static Robot robot;

    // Press Function
    public void Press(int key, int time){ 
        try {
        Robot robot = new Robot();
            robot.keyPress(key);
            robot.delay(time);
            robot.keyRelease(key);

        } catch (AWTException e) {
            e.printStackTrace();
        }
    } 

}

And my Example.java code is: 我的Example.java代码是:

package com.z;

import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class Example {

    public static void main(String[] args) {

        RobotLib robot = new RobotLib();

        robot.Press(KeyEvent.VK_A,100); // type a

    }
}

With the RobotLib.java class I was wondering if it's possible to have the functions without wrapping them with try/catch and new Robot() so instead of the above version it would be something like this instead: 使用RobotLib.java类,我想知道是否可以使用try / catch和新的Robot()来包装它们的函数,所以不是上面的版本,它将是这样的:

public void Press(int key, int time){ 
    robot.keyPress(key);
    robot.delay(time);
    robot.keyRelease(key);
}

The try/catch and new Robot() seem to be required however and if I take those away I get errors like this: 然而,似乎需要try / catch和new Robot(),如果我把它拿走,我会得到这样的错误:

Exception in thread "main" java.lang.NullPointerException
    at com.z.RobotLib.Press(RobotLib.java:35)
    at com.z.Example.main(Example.java:14)

I am quite new to Java coding and might be setting up the class the wrong way, is there a way to fix that error or have the functions how I want? 我是Java编码的新手,可能是以错误的方式设置类,有没有办法解决这个错误或者有我想要的功能?

Yes you do need the try/catch block in there, but yes there is also a way to set up those functions better. 是的,你确实需要try / catch块,但是也有一种方法可以更好地设置这些功能。 You don't need to create a robot each time you call the Press method. 每次调用Press方法时都不需要创建机器人。 Create your static Robot instance in your constructor. 在构造函数中创建静态Robot实例。

public class RobotLib {

private static Robot robot;

public RobotLib(){
  robot = new Robot();
}

// Press Function
public void Press(int key, int time){ 
    try {
        robot.keyPress(key);
        robot.delay(time);
        robot.keyRelease(key);

    } catch (AWTException e) {
        e.printStackTrace();
    }
} 

}

Don't quite sure of your questions but I hope this might help! 不太确定你的问题,但我希望这可能会有所帮助!

You can throws an exception to avoid unnecessary try and catch blocks. 您可以抛出异常以避免不必要的try和catch块。 Also, creating an instance of class Robot will make you avoid writing new Robot() at everyline you are in need of it. 此外,创建类Robot的实例将使您避免在需要它的每一行上编写新的Robot()。

I just found a way to do what i wanted using a modified version of the code Raskolnikov posted, it allows shorter versions of the function like i wanted: 我刚刚发现了一种方法,可以使用Raskolnikov发布的代码的修改版本做我想要的,它允许像我想要的更短版本的功能:

public class RobotLib {

private static Robot robot;

public RobotLib(){
  try {
    robot = new Robot();
} catch (AWTException e) {
    e.printStackTrace();
}
}

// Press Function
public void Press(int key, int time){ 
    robot.keyPress(key);
    robot.delay(time);
    robot.keyRelease(key);
} 

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM