【问题标题】:Running a Swing application programmatically (remote classes)以编程方式运行 Swing 应用程序(远程类)
【发布时间】:2011-02-27 19:47:31
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: java swing awt awtrobot


    【解决方案1】:

    如果您只知道在哪里点击,那么在您自己的机器人应用程序中也不是问题。它通常只需要一个启动条件 - 实际程序在屏幕上的位置。

    这可能会帮助您继续前进:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 1970-01-01
      • 1970-01-01
      • 2012-04-25
      相关资源
      最近更新 更多