java.awt Class Robot java.lang.Object | +-java.awt.Robotpublic class Robot extends Object This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations. 使用这个类完全可以满足你的要求
以上是个例子,仓促写成,请多指教import java.awt.*; import java.awt.event.*; import javax.swing.*;public class Test extends JFrame { private JButton button = new JButton("show dialog ..."); private String message = "Please Enter Your Name"; public Test() { super("Test Robot"); Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout()); contentPane.add(button,BorderLayout.CENTER); setLocation(400,300); setSize(200,100); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String s = JOptionPane.showInputDialog(message); if(s == null) JOptionPane.showMessageDialog(Test.this,"cancel button activated"); else JOptionPane.showMessageDialog(Test.this,"Name: " + s); } }); } public static void moveSmooth(Robot robot, int x1, int y1, int x2, int y2, int stop) { for(int i = 0;i<stop;i++) { robot.mouseMove(x1+(x2-x1)/stop*i,y1+(y2-y1)/stop*i); robot.delay(100); } } public static void main(String[] args) throws Exception { Test test = new Test(); test.show(); Robot robot = new Robot(); robot.delay(5000); moveSmooth(robot,200,200,500,350,30); robot.delay(1000); robot.mousePress(InputEvent.BUTTON1_MASK); robot.delay(200); robot.mouseRelease(InputEvent.BUTTON1_MASK); robot.waitForIdle(); robot.setAutoDelay(200); robot.keyPress(KeyEvent.VK_F); robot.keyRelease(KeyEvent.VK_F); robot.keyPress(KeyEvent.VK_I); robot.keyRelease(KeyEvent.VK_I); robot.keyPress(KeyEvent.VK_G); robot.keyRelease(KeyEvent.VK_G); robot.keyPress(KeyEvent.VK_H); robot.keyRelease(KeyEvent.VK_H); robot.keyPress(KeyEvent.VK_T); robot.keyRelease(KeyEvent.VK_T); robot.keyPress(KeyEvent.VK_B); robot.keyRelease(KeyEvent.VK_B); robot.keyPress(KeyEvent.VK_O); robot.keyRelease(KeyEvent.VK_O); robot.keyPress(KeyEvent.VK_Y); robot.keyRelease(KeyEvent.VK_Y); robot.delay(2000); robot.keyPress(KeyEvent.VK_ENTER); robot.keyRelease(KeyEvent.VK_ENTER); } }
Class Robot
java.lang.Object
|
+-java.awt.Robotpublic class Robot
extends Object
This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.
使用这个类完全可以满足你的要求
import java.awt.event.*;
import javax.swing.*;public class Test extends JFrame {
private JButton button = new JButton("show dialog ...");
private String message = "Please Enter Your Name"; public Test() {
super("Test Robot");
Container contentPane = getContentPane(); contentPane.setLayout(new BorderLayout());
contentPane.add(button,BorderLayout.CENTER); setLocation(400,300);
setSize(200,100);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s = JOptionPane.showInputDialog(message); if(s == null)
JOptionPane.showMessageDialog(Test.this,"cancel button activated");
else
JOptionPane.showMessageDialog(Test.this,"Name: " + s);
}
});
} public static void moveSmooth(Robot robot,
int x1, int y1,
int x2, int y2,
int stop)
{
for(int i = 0;i<stop;i++)
{
robot.mouseMove(x1+(x2-x1)/stop*i,y1+(y2-y1)/stop*i);
robot.delay(100);
}
} public static void main(String[] args) throws Exception
{
Test test = new Test();
test.show();
Robot robot = new Robot();
robot.delay(5000);
moveSmooth(robot,200,200,500,350,30);
robot.delay(1000);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(200);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.waitForIdle();
robot.setAutoDelay(200);
robot.keyPress(KeyEvent.VK_F);
robot.keyRelease(KeyEvent.VK_F);
robot.keyPress(KeyEvent.VK_I);
robot.keyRelease(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_G);
robot.keyRelease(KeyEvent.VK_G);
robot.keyPress(KeyEvent.VK_H);
robot.keyRelease(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_T);
robot.keyPress(KeyEvent.VK_B);
robot.keyRelease(KeyEvent.VK_B);
robot.keyPress(KeyEvent.VK_O);
robot.keyRelease(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_Y);
robot.keyRelease(KeyEvent.VK_Y);
robot.delay(2000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
}