2010-06-04 59 views
3

運行Swing應用程序我有一個稍微複雜的情況下,我沒有源代碼(或編譯的類),我想自動運行的Swing應用程序。編程(遠程類)

我會盡力做了一系列任務的這個應用程序,按下幾個按鈕,點擊一些零件等。我希望能夠以編程方式做到這一點。

每一個鞦韆調試器/機器人我所遇到希望你有你的啓動類和調試器與類一起啓動。這裏

問題是我的應用程序是由我發起一個JNLP應用程序,它驗證了我推出(我必須輸入用戶名和密碼),然後運行一個遠程服務器上的一堆類。 Swing應用程序啓動。

我想是在一個地步,我現在可以或許連接到Swing應用程序編程和運行它。很抱歉,這似乎太複雜了,但是這是這裏的情景......

也許是沒有辦法做到這一點在所有的,請告訴我,這樣還,如果是這樣的話......

回答

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