下面列出了java.awt.Robot#mouseMove ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String... args) throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(20);
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
new bug4743225().setVisible(true);
}
});
toolkit.realSync();
// calling this method from main thread is ok
Point point = cb.getLocationOnScreen();
robot.mouseMove(point.x + 10, point.y + 10);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
toolkit.realSync();
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
if(getPopup().getList().getLastVisibleIndex() == 3) {
flag = true;
}
}
});
if (!flag) {
throw new RuntimeException("The ComboBox popup wasn't correctly updated");
}
}
public static void main(final String[] args) throws Exception {
try {
final Robot r = new Robot();
r.setAutoDelay(50);
r.mouseMove(100, 100);
Util.waitForIdle(r);
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
initAndShowUI();
}
});
final Point inside = new Point(frame.getLocationOnScreen());
inside.translate(20, SIZE / 2);
final Point outer = new Point(inside);
outer.translate(-40, 0);
r.mouseMove(inside.x, inside.y);
r.mousePress(InputEvent.BUTTON1_MASK);
try {
for (int i = 0; i < 3; ++i) {
Util.mouseMove(r, inside, outer);
Util.mouseMove(r, outer, inside);
}
} finally {
r.mouseRelease(InputEvent.BUTTON1_MASK);
}
sleep(r);
if (FAILED || !MOUSE_ENTERED || !MOUSE_ENTERED_DT || !MOUSE_EXIT
|| !MOUSE_EXIT_TD) {
throw new RuntimeException("Failed");
}
} finally {
if (frame != null) {
frame.dispose();
}
}
}
public static void main(String[] args) throws Exception {
Frame sourceFrame = createFrame("Source Frame", 0, 0);
Frame targetFrame = createFrame("Target Frame", 250, 250);
DragSource defaultDragSource
= DragSource.getDefaultDragSource();
defaultDragSource.createDefaultDragGestureRecognizer(sourceFrame,
DnDConstants.ACTION_COPY_OR_MOVE,
new TestDragGestureListener());
new DropTarget(targetFrame, DnDConstants.ACTION_COPY_OR_MOVE,
new TestDropTargetListener(targetFrame));
Robot robot = new Robot();
robot.setAutoDelay(50);
sourceFrame.toFront();
robot.waitForIdle();
Point point = getCenterPoint(sourceFrame);
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
mouseDragAndDrop(robot, point, getCenterPoint(targetFrame));
long time = System.currentTimeMillis() + 1000;
while (!passed) {
if (time < System.currentTimeMillis()) {
sourceFrame.dispose();
targetFrame.dispose();
throw new RuntimeException("Mouse clicked event is lost!");
}
Thread.sleep(10);
}
sourceFrame.dispose();
targetFrame.dispose();
}
public void press() throws AWTException {
Point point = this.table.getCellRect(1, 1, false).getLocation();
SwingUtilities.convertPointToScreen(point, this.table);
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.mouseMove(point.x + 1, point.y + 1);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
private static void runTest(Dialog dialog, Frame frame) {
try {
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.mouseMove(300, 300);
while (!dialog.isVisible()) {
sleep();
}
Rectangle dialogBounds = dialog.getBounds();
Rectangle frameBounds = frame.getBounds();
int y1 = dialogBounds.y + dialogBounds.height;
int y2 = frameBounds.y + frameBounds.height;
int clickX = frameBounds.x + frameBounds.width / 2;
int clickY = y1 + (y2 - y1) / 2;
robot.mouseMove(clickX, clickY);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
sleep();
int colorX = dialogBounds.x + dialogBounds.width / 2;
int colorY = dialogBounds.y + dialogBounds.height / 2;
Color color = robot.getPixelColor(colorX, colorY);
dialog.dispose();
frame.dispose();
if (!DIALOG_COLOR.equals(color)) {
throw new RuntimeException("The frame is on top"
+ " of the modal dialog!");
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static void main(String[] args) throws Exception {
Frame sourceFrame = createFrame("Source Frame", 0, 0);
Frame targetFrame = createFrame("Target Frame", 250, 250);
DragSource defaultDragSource
= DragSource.getDefaultDragSource();
defaultDragSource.createDefaultDragGestureRecognizer(sourceFrame,
DnDConstants.ACTION_COPY_OR_MOVE,
new TestDragGestureListener());
new DropTarget(targetFrame, DnDConstants.ACTION_COPY_OR_MOVE,
new TestDropTargetListener(targetFrame));
Robot robot = new Robot();
robot.setAutoDelay(50);
sourceFrame.toFront();
robot.waitForIdle();
Point point = getCenterPoint(sourceFrame);
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
mouseDragAndDrop(robot, point, getCenterPoint(targetFrame));
long time = System.currentTimeMillis() + 200;
while (!passed) {
if (time < System.currentTimeMillis()) {
sourceFrame.dispose();
targetFrame.dispose();
throw new RuntimeException("Mouse clicked event is lost!");
}
Thread.sleep(10);
}
sourceFrame.dispose();
targetFrame.dispose();
}
public static void main(String... args) throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(20);
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
new bug4743225().setVisible(true);
}
});
toolkit.realSync();
// calling this method from main thread is ok
Point point = cb.getLocationOnScreen();
robot.mouseMove(point.x + 10, point.y + 10);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
toolkit.realSync();
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
if(getPopup().getList().getLastVisibleIndex() == 3) {
flag = true;
}
}
});
if (!flag) {
throw new RuntimeException("The ComboBox popup wasn't correctly updated");
}
}
private static void test(TestItem item) throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(50);
SwingUtilities.invokeAndWait(() -> createAndShowGUI(item));
robot.waitForIdle();
Point point = getClickPoint(true);
robot.mouseMove(point.x, point.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.waitForIdle();
point = getClickPoint(false);
robot.mouseMove(point.x, point.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> {
JMenuItem menuItem = menu.getItem(0);
boolean isShowing = menuItem.isShowing();
frame.dispose();
if (isShowing ^ item.doNotCloseOnMouseClick()) {
throw new RuntimeException("Property is not taken into account!");
}
});
}
/**
* Move the mouse at (X,Y) screen position and then click the mouse button 1
*
* @param x the X coordinate
* @param y the Y coordinate
*/
public void clickAt( int x, int y ) {
Robot robot = getRobot();
robot.mouseMove(x, y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.delay(500);
}
public static void main(final String[] args) throws Exception {
try {
final Robot r = new Robot();
r.setAutoDelay(50);
r.mouseMove(100, 100);
Util.waitForIdle(r);
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
initAndShowUI();
}
});
final Point inside = new Point(frame.getLocationOnScreen());
inside.translate(20, SIZE / 2);
final Point outer = new Point(inside);
outer.translate(-40, 0);
r.mouseMove(inside.x, inside.y);
r.mousePress(InputEvent.BUTTON1_MASK);
try {
for (int i = 0; i < 3; ++i) {
Util.mouseMove(r, inside, outer);
Util.mouseMove(r, outer, inside);
}
} finally {
r.mouseRelease(InputEvent.BUTTON1_MASK);
}
sleep();
if (FAILED || !MOUSE_ENTERED || !MOUSE_ENTERED_DT || !MOUSE_EXIT
|| !MOUSE_EXIT_TD) {
throw new RuntimeException("Failed");
}
} finally {
if (frame != null) {
frame.dispose();
}
}
}
private static void drag(Robot r, Point startPt, Point endPt, int button) {
if (!(button == InputEvent.BUTTON1_MASK
|| button == InputEvent.BUTTON2_MASK
|| button == InputEvent.BUTTON3_MASK)) {
throw new IllegalArgumentException("invalid mouse button");
}
r.mouseMove(startPt.x, startPt.y);
r.mousePress(button);
try {
mouseMove(r, startPt, endPt);
} finally {
r.mouseRelease(button);
}
}
public void firstShowPopup() throws Exception {
Point point = this.panel.getLocation();
SwingUtilities.convertPointToScreen(point, this.panel);
robot = new Robot(); // initialize shared static field first time
robot.mouseMove(point.x + 1, point.y + 1);
robot.mousePress(InputEvent.BUTTON3_MASK);
Thread.currentThread().setUncaughtExceptionHandler(this);
robot.mouseRelease(InputEvent.BUTTON3_MASK); // causes first AssertionError on EDT
}
public static void main(final String[] args) throws Exception {
try {
final Robot r = new Robot();
r.setAutoDelay(50);
r.mouseMove(100, 100);
Util.waitForIdle(r);
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
initAndShowUI();
}
});
final Point inside = new Point(frame.getLocationOnScreen());
inside.translate(20, SIZE / 2);
final Point outer = new Point(inside);
outer.translate(-40, 0);
r.mouseMove(inside.x, inside.y);
r.mousePress(InputEvent.BUTTON1_MASK);
try {
for (int i = 0; i < 3; ++i) {
Util.mouseMove(r, inside, outer);
Util.mouseMove(r, outer, inside);
}
} finally {
r.mouseRelease(InputEvent.BUTTON1_MASK);
}
sleep();
if (FAILED || !MOUSE_ENTERED || !MOUSE_ENTERED_DT || !MOUSE_EXIT
|| !MOUSE_EXIT_TD) {
throw new RuntimeException("Failed");
}
} finally {
if (frame != null) {
frame.dispose();
}
}
}
private static void rmove(Robot robot, Point p) {
robot.mouseMove(p.x, p.y);
}
public static void main(String[] args) throws AWTException
{
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gds = ge.getScreenDevices();
if (gds.length < 2) {
System.out.println("It's a multiscreen test... skipping!");
return;
}
for (int i = 0; i < gds.length; ++i) {
GraphicsDevice gd = gds[i];
GraphicsConfiguration gc = gd.getDefaultConfiguration();
Rectangle screen = gc.getBounds();
Robot robot = new Robot(gd);
// check Robot.mouseMove()
robot.mouseMove(screen.x + mouseOffset.x, screen.y + mouseOffset.y);
Point mouse = MouseInfo.getPointerInfo().getLocation();
Point point = screen.getLocation();
point.translate(mouseOffset.x, mouseOffset.y);
if (!point.equals(mouse)) {
throw new RuntimeException(getErrorText("Robot.mouseMove", i));
}
// check Robot.getPixelColor()
Frame frame = new Frame(gc);
frame.setUndecorated(true);
frame.setSize(100, 100);
frame.setLocation(screen.x + frameOffset.x, screen.y + frameOffset.y);
frame.setBackground(color);
frame.setVisible(true);
robot.waitForIdle();
Rectangle bounds = frame.getBounds();
if (!Util.testBoundsColor(bounds, color, 5, 1000, robot)) {
throw new RuntimeException(getErrorText("Robot.getPixelColor", i));
}
// check Robot.createScreenCapture()
BufferedImage image = robot.createScreenCapture(bounds);
int rgb = color.getRGB();
if (image.getRGB(0, 0) != rgb
|| image.getRGB(image.getWidth() - 1, 0) != rgb
|| image.getRGB(image.getWidth() - 1, image.getHeight() - 1) != rgb
|| image.getRGB(0, image.getHeight() - 1) != rgb) {
throw new RuntimeException(
getErrorText("Robot.createScreenCapture", i));
}
frame.dispose();
}
System.out.println("Test PASSED!");
}
public static void main(String[] args) throws Exception {
try {
//Move the mouse out, because it could interfere with the test.
Robot r = new Robot();
r.setAutoDelay(50);
r.mouseMove(0, 0);
sleep();
SwingUtilities.invokeAndWait(FullScreenAfterSplash::createAndShowGUI);
sleep();
Point fullScreenButtonPos = frame.getLocation();
if(System.getProperty("os.version").equals("10.9"))
fullScreenButtonPos.translate(frame.getWidth() - 10, frame.getHeight()/2);
else
fullScreenButtonPos.translate(55,frame.getHeight()/2);
r.mouseMove(fullScreenButtonPos.x, fullScreenButtonPos.y);
//Cant use waitForIdle for full screen transition.
int waitCount = 0;
while (!windowEnteringFullScreen) {
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(100);
if (waitCount++ > 10) {
System.err.println("Can't enter full screen mode. Failed.");
System.exit(1);
}
}
waitCount = 0;
while (!windowEnteredFullScreen) {
Thread.sleep(100);
if (waitCount++ > 10) {
System.err.println("Can't enter full screen mode. Failed.");
System.exit(1);
}
}
} finally {
if (frame != null) {
frame.dispose();
}
}
}
public void start() {
Frame f = new Frame("Use keyboard for DnD change");
Panel mainPanel;
Component dragSource, dropTarget;
f.setBounds(0, 400, 200, 200);
f.setLayout(new BorderLayout());
mainPanel = new Panel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setBackground(Color.blue);
dropTarget = new DnDTarget(Color.red, Color.yellow);
dragSource = new DnDSource("Drag ME! (" + (DragSource.isDragImageSupported()?"with ":"without") + " image)" );
mainPanel.add(dragSource, "North");
mainPanel.add(dropTarget, "Center");
f.add(mainPanel, BorderLayout.CENTER);
f.setVisible(true);
try {
Point sourcePoint = dragSource.getLocationOnScreen();
Dimension d = dragSource.getSize();
sourcePoint.translate(d.width / 2, d.height / 2);
Robot robot = new Robot();
robot.mouseMove(sourcePoint.x, sourcePoint.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
Thread.sleep(2000);
for(int i = 0; i <100; ++i) {
robot.mouseMove(
sourcePoint.x + d.width / 2 + 10,
sourcePoint.y + d.height);
Thread.sleep(100);
robot.mouseMove(sourcePoint.x, sourcePoint.y);
Thread.sleep(100);
robot.mouseMove(
sourcePoint.x,
sourcePoint.y + d.height);
Thread.sleep(100);
}
sourcePoint.y += d.height;
robot.mouseMove(sourcePoint.x, sourcePoint.y);
Thread.sleep(100);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(4000);
} catch( Exception e){
e.printStackTrace();
throw new RuntimeException("test failed: drop was not successful with exception " + e);
}
}
public void start() {
Frame f = new Frame("Use keyboard for DnD change");
Panel mainPanel;
Component dragSource, dropTarget;
f.setBounds(0, 400, 200, 200);
f.setLayout(new BorderLayout());
mainPanel = new Panel();
mainPanel.setLayout(new BorderLayout());
mainPanel.setBackground(Color.blue);
dropTarget = new DnDTarget(Color.red, Color.yellow);
dragSource = new DnDSource("Drag ME! (" + (DragSource.isDragImageSupported()?"with ":"without") + " image)" );
mainPanel.add(dragSource, "North");
mainPanel.add(dropTarget, "Center");
f.add(mainPanel, BorderLayout.CENTER);
f.setVisible(true);
try {
Point sourcePoint = dragSource.getLocationOnScreen();
Dimension d = dragSource.getSize();
sourcePoint.translate(d.width / 2, d.height / 2);
Robot robot = new Robot();
robot.mouseMove(sourcePoint.x, sourcePoint.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
Thread.sleep(2000);
for(int i = 0; i <100; ++i) {
robot.mouseMove(
sourcePoint.x + d.width / 2 + 10,
sourcePoint.y + d.height);
Thread.sleep(100);
robot.mouseMove(sourcePoint.x, sourcePoint.y);
Thread.sleep(100);
robot.mouseMove(
sourcePoint.x,
sourcePoint.y + d.height);
Thread.sleep(100);
}
sourcePoint.y += d.height;
robot.mouseMove(sourcePoint.x, sourcePoint.y);
Thread.sleep(100);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(4000);
} catch( Exception e){
e.printStackTrace();
throw new RuntimeException("test failed: drop was not successful with exception " + e);
}
}
public static void main(String[] args) throws Exception {
// Windows only test
if (OSInfo.getOSType() == OSInfo.OSType.WINDOWS) {
// Retrieving top edge of Desktop
GraphicsConfiguration grConf = GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice()
.getDefaultConfiguration();
Rectangle scrRect = grConf.getBounds();
Insets scrInsets = Toolkit.getDefaultToolkit().getScreenInsets(grConf);
scrTop = scrRect.y + scrInsets.top;
color = new Color(0, 255, 0);
SwingUtilities.invokeAndWait(() -> {
createAndShowUI();
});
try {
Robot robot = new Robot();
robot.setAutoDelay(500);
robot.setAutoWaitForIdle(true);
robot.delay(1000);
// Resizing a window to invoke Windows Snap feature
readFrameInfo();
robot.mouseMove(frLoc.x + frSize.width / 2, frLoc.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseMove(frLoc.x + frSize.width / 2, scrTop);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
// Retrieving the color of window expanded area
readFrameInfo();
Insets insets = frame.getInsets();
Color bgColor = robot.getPixelColor(frLoc.x + frSize.width / 2,
frLoc.y + frSize.height - insets.bottom - 1);
frame.dispose();
if (!bgColor.equals(color)) {
throw new RuntimeException("TEST FAILED: got "
+ bgColor + " instead of " + color);
}
System.out.println("TEST PASSED!");
} catch (AWTException ex) {
throw new RuntimeException("TEST FAILED!");
}
}
}
/**
* Moves mouse pointer in the center of given {@code comp} component
* using {@code robot} parameter.
*/
public static void pointOnComp(final Component comp, final Robot robot) {
Rectangle bounds = new Rectangle(comp.getLocationOnScreen(), comp.getSize());
robot.mouseMove(bounds.x + bounds.width / 2, bounds.y + bounds.height / 2);
}