3

自動的に実行しようとしているスイング アプリケーションのソース コード (またはコンパイルされたクラス) がないという、少し複雑なケースがあります。

このアプリケーションで一連のタスクを実行し、いくつかのボタンを押したり、いくつかの部分をクリックしたりします。これをプログラムで実行できるようにしたいと考えています。

私が遭遇したすべての Swing デバッガー/ロボットは、起動しているクラスを持っていることを望んでおり、デバッガーはクラスと一緒に起動します。

ここでの問題は、JNLP アプリケーションを起動することによってアプリケーションが起動されることです。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 に答える