下面列出了java.awt.Robot#mouseRelease ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void main(String[] args) throws Exception {
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
Robot robot = new Robot();
robot.setAutoDelay(50);
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
toolkit.realSync();
Point point = getRowPointToClick(2);
robot.mouseMove(point.x, point.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
toolkit.realSync();
}
public static void mouseDND(Robot robot, int x1, int y1, int x2, int y2) {
int N = 20;
int x = x1;
int y = y1;
int dx = (x2 - x1) / N;
int dy = (y2 - y1) / N;
robot.mousePress(InputEvent.BUTTON1_MASK);
for (int i = 0; i < N; i++) {
robot.mouseMove(x += dx, y += dy);
}
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
private static void clickOnDialog(Dialog dialog) {
try {
long time = System.currentTimeMillis() + 200;
while (!dialog.isVisible()) {
if (time < System.currentTimeMillis()) {
throw new RuntimeException("Dialog is not visible!");
}
Thread.sleep(10);
}
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.waitForIdle();
robot.delay(200);
Point point = getCenterPoint(dialog);
robot.mouseMove(point.x, point.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* Drags from one point to another with the specified mouse button pressed.
*
* @param robot a robot to use for moving the mouse, etc.
* @param startPoint a start point of the drag
* @param endPoint an end point of the drag
* @param button one of {@code InputEvent.BUTTON1_MASK},
* {@code InputEvent.BUTTON2_MASK}, {@code InputEvent.BUTTON3_MASK}
*
* @throws IllegalArgumentException if {@code button} is not one of
* {@code InputEvent.BUTTON1_MASK}, {@code InputEvent.BUTTON2_MASK},
* {@code InputEvent.BUTTON3_MASK}
*/
public static void drag(Robot robot, Point startPoint, Point endPoint, int button) {
if (!(button == InputEvent.BUTTON1_MASK || button == InputEvent.BUTTON2_MASK
|| button == InputEvent.BUTTON3_MASK))
{
throw new IllegalArgumentException("invalid mouse button");
}
robot.mouseMove(startPoint.x, startPoint.y);
robot.mousePress(button);
try {
mouseMove(robot, startPoint, endPoint);
} finally {
robot.mouseRelease(button);
}
}
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(50);
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
robot.waitForIdle();
Point point = getRowPointToClick(2);
robot.mouseMove(point.x, point.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.waitForIdle();
}
private static void clickOnDialog(Dialog dialog) {
try {
long time = System.currentTimeMillis() + 200;
while (!dialog.isVisible()) {
if (time < System.currentTimeMillis()) {
throw new RuntimeException("Dialog is not visible!");
}
Thread.sleep(10);
}
Point point = getCenterPoint(dialog);
Robot robot = new Robot();
robot.setAutoDelay(50);
robot.mouseMove(point.x, point.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(50);
SwingUtilities.invokeAndWait(bug8072767::createAndShowGUI);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> {
point = table.getLocationOnScreen();
Rectangle rect = table.getCellRect(0, 0, true);
point.translate(rect.width / 2, rect.height / 2);
});
robot.mouseMove(point.x, point.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> {
point = frame.getLocationOnScreen();
point.translate(frame.getWidth() / 2, frame.getHeight() / 2);
});
robot.mouseMove(point.x, point.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> {
testPass = TEST2.equals(table.getValueAt(0, 0));
frame.dispose();
});
if (!testPass) {
throw new RuntimeException("Table cell is not edited!");
}
}
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 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
}
static void mouseClickOnComp(Robot r, Component comp) {
Point loc = comp.getLocationOnScreen();
loc.x += comp.getWidth() / 2;
loc.y += comp.getHeight() / 2;
r.mouseMove(loc.x, loc.y);
r.delay(10);
r.mousePress(InputEvent.BUTTON1_MASK);
r.delay(10);
r.mouseRelease(InputEvent.BUTTON1_MASK);
}
/**
* Moves mouse pointer in the center of a given {@code comp} component
* and performs a left mouse button click using the {@code robot} parameter
* with the {@code delay} delay between press and release.
*/
public static void clickOnComp(final Component comp, final Robot robot, int delay) {
pointOnComp(comp, robot);
robot.delay(delay);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(delay);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
}
static void mouseClickOnComp(Robot r, Component comp) {
Point loc = comp.getLocationOnScreen();
loc.x += comp.getWidth() / 2;
loc.y += comp.getHeight() / 2;
r.mouseMove(loc.x, loc.y);
r.delay(10);
r.mousePress(InputEvent.BUTTON1_MASK);
r.delay(10);
r.mouseRelease(InputEvent.BUTTON1_MASK);
}
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 void test() throws Exception {
Robot robot = new Robot();
SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
UIManager.setLookAndFeel(info.getClassName());
SwingUtilities.invokeAndWait(this);
toolkit.realSync(); // after creation
Thread.sleep(1000);
Point point = this.bar.getLocation();
SwingUtilities.convertPointToScreen(point, this.bar);
point.x += this.bar.getWidth() >> 2;
point.y += this.bar.getHeight() >> 1;
robot.mouseMove(point.x, point.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
toolkit.realSync(); // before validation
Thread.sleep(1000);
SwingUtilities.invokeAndWait(this);
if (this.bar != null) {
this.bar = null; // allows to reuse the instance
if (AUTO) { // error reporting only for automatic testing
throw new Error("TEST FAILED");
}
}
}
}
public static void clickOnTitle(final Window decoratedWindow, final Robot robot) {
if (decoratedWindow instanceof Frame || decoratedWindow instanceof Dialog) {
Point p = getTitlePoint(decoratedWindow);
robot.mouseMove(p.x, p.y);
robot.delay(50);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.delay(50);
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 void movedGeneratesSameEvents() throws Throwable {
events = MouseEvent.MOUSE_MOVED;
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
actionsArea.setText("");
}
});
driver = new JavaDriver();
WebElement b = driver.findElement(By.name("click-me"));
WebElement t = driver.findElement(By.name("actions"));
Point location = EventQueueWait.call_noexc(button, "getLocationOnScreen");
Dimension size = EventQueueWait.call_noexc(button, "getSize");
Robot r = new Robot();
r.setAutoDelay(10);
r.setAutoWaitForIdle(true);
Point location2 = EventQueueWait.call_noexc(actionsArea, "getLocationOnScreen");
Dimension size2 = EventQueueWait.call_noexc(actionsArea, "getSize");
r.mouseMove(location2.x + size2.width / 2, location2.y + size2.height / 2);
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
r.mouseMove(location.x + size.width / 2, location.y + size.height / 2);
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
new EventQueueWait() {
@Override
public boolean till() {
return actionsArea.getText().length() > 0;
}
}.wait("Waiting for actionsArea failed?");
String expected = t.getText();
tclear();
r.mouseMove(location2.x + size2.width / 2, location2.y + size2.height / 2);
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
b.click();
AssertJUnit.assertEquals(expected, t.getText());
tclear();
new Actions(driver).moveToElement(b).click().perform();
AssertJUnit.assertEquals(expected, t.getText());
}
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 {
if (OSInfo.getOSType() != OSInfo.OSType.MACOSX) {
System.out.println("The test is applicable only to Mac OS X. Passed");
return;
}
try {
//Move the mouse out, because it could interfere with the test.
Robot r = new Robot();
r.mouseMove(0, 0);
sleep();
SwingUtilities.invokeAndWait(FullScreenAfterSplash::createAndShowGUI);
sleep();
Point fullScreenButtonPos = frame.getLocation();
fullScreenButtonPos.translate(frame.getWidth() - 10, 10);
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();
}
}
}
@Override
protected boolean performTest() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
heavyLoc = currentAwtControl.getLocationOnScreen();
}
});
} catch (Exception e) {
}
Robot robot = Util.createRobot();
robot.setAutoDelay(ROBOT_DELAY);
Util.waitForIdle(robot);
// Move the mouse pointer to the position where both
// components overlap
robot.mouseMove(heavyLoc.x + 5, heavyLoc.y + 5);
// Now perform the click at this point for 9 times
// In the middle of the process toggle the opaque
// flag value.
for (int i = 0; i < 9; ++i) {
if (i == 3) {
light.setMixingCutoutShape(new Rectangle());
}
if (i == 6) {
light.setMixingCutoutShape(null);
}
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Util.waitForIdle(robot);
if (currentAwtControl.getClass() == java.awt.Choice.class && i != 1 && i != 6 && i != 8) {
// due to the fact that Choice doesn't get mouseClicked event if its dropdown is shown
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Util.waitForIdle(robot);
}
}
Util.waitForIdle(robot);
boolean result = testSeq.equals(checkSeq);
if (!result) {
System.err.println("Expected: " + checkSeq);
System.err.println("Observed: " + testSeq);
}
return result;
}