3

我有一个稍微复杂的情况,我没有尝试自动运行的 Swing 应用程序的源代码(或编译的类)。

我将尝试在此应用程序上执行一系列任务,按一些按钮,单击某些部分等。我希望能够以编程方式执行此操作。

我遇到的每一个 Swing 调试器/机器人都希望您拥有正在启动的类,并且调试器与该类一起启动。

这里的问题是我的应用程序是由我启动一个 JNLP 应用程序启动的,该应用程序对我进行身份验证(我必须输入用户名和密码),然后在远程服务器上运行一堆类。Swing 应用程序启动。

我希望现在可以附加到 Swing 应用程序并以编程方式运行它。抱歉,这似乎太复杂了,但这就是这里的场景......

也许根本没有办法做到这一点,如果是这样的话,请告诉我......

4

1 回答 1

2

如果您只知道在哪里单击,那么做您自己的机器人应用程序不是问题。它通常只需要一个开始标准 - 实际程序在屏幕上的位置。

这可能会帮助您开始:

public class MyRobot extends Robot {

    public MyRobot(Point initialLocation) throws AWTException {

        setAutoDelay(20);

        // focus on the program
        click(initialLocation);

        // if you need to take screen shot use 
        BufferedImage screen = 
            createScreenCapture(
                new Rectangle(initialLocation.x, initialLocation.y, 200, 200));

        // analyze the screenshot...
        if(screen.getRGB(50, 50) > 3) /*do something :) */;


        // go to the correct field
        press(KeyEvent.VK_TAB);

        // press "a"
        press(KeyEvent.VK_A);

        // go to the next field
        press(KeyEvent.VK_TAB);

        // write something...
        type("Hello World..");
    }

    private void click(Point p) {
        mousePress(InputEvent.BUTTON1_MASK);
        mouseRelease(InputEvent.BUTTON1_MASK);
    }

    private void press(int key) {
        keyPress(key);
        keyRelease(key);
    }

    private void type(String string) {
        // quite complicated... see 
        //http://stackoverflow.com/questions/1248510/convert-string-to-keyevents
    }

    @SuppressWarnings("serial")
    public static void main(String[] args) throws Exception {
        final JDialog d = new JDialog();
        d.setTitle("Init");
        d.add(new JButton(
                "Put your mouse above the 'program' " +
                "and press this button") {
            {
            addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    synchronized (d) { d.notify(); }
                    d.dispose();
                }
            });}
        });
        d.setSize(200, 100);
        d.setVisible(true);
        // wait for it to be closed
        synchronized (d) {
            d.wait();
        }
        new MyRobot(MouseInfo.getPointerInfo().getLocation());
    }
}
于 2010-06-04T14:38:31.783 回答