以编程方式运行 Swing 应用程序(远程类)

发布于 2024-09-04 05:23:47 字数 366 浏览 6 评论 0原文

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

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

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

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

我希望现在可以附加到 swing 应用程序并以编程方式运行它。抱歉,这看起来太复杂了,但这就是这里的情况...

也许根本没有办法做到这一点,如果是这种情况,也请告诉我...

I have a slightly complicated case where I do not have source code (or the compiled class) the swing application that I am trying to run automatically.

I will try to do a series of tasks on this application, press some buttons, click on some parts etc. I want to be able to do this programatically.

Every single Swing debugger/robot I have come across wants you to have the class you are launching and the debugger launches together with the class.

Problem here is my application is launched by me launching a JNLP application, which authenticates me (I have to enter username and password), then runs a bunch of classes on a remote server. And the Swing application gets launched.

I want to be at a point where I can now perhaps attach to the swing application and run it programatically. Sorry this seems too complicated, but this is the scenario here...

Perhaps there is no way to do it at all, please tell me so also if that is the case...

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

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

发布评论

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

评论(1

伤痕我心 2024-09-11 05:23:47

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

这可能会帮助您开始:

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());
    }
}

If you just know where to click it's not a problem doing your own Robot application. It normally only needs a start criteria - where the actual program is on the screen.

This might help you get going:

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