下面列出了java.awt.Robot#mouseWheel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void mouseWheel(int count) {
// Event event = new Event();
// event.type = SWT.MouseWheel;
// event.data = VTracker.getActiveControl();
// event.detail = SWT.SCROLL_LINE;
// event.x = x;
// event.y = y;
// event.count = count;
// display.post(event);
try {
Robot robot = new Robot();
robot.mouseWheel(count);
} catch (Exception e) {
e.printStackTrace();
}
pause(delay);
}
private static void test(GraphicsConfiguration gc) throws AWTException {
final Window frame = new Frame(gc);
try {
frame.addMouseWheelListener(e -> {
wheelX = e.getXOnScreen();
wheelY = e.getYOnScreen();
done = true;
});
frame.setSize(300, 300);
frame.setVisible(true);
final Robot robot = new Robot();
robot.setAutoDelay(50);
robot.setAutoWaitForIdle(true);
mouseX = frame.getX() + frame.getWidth() / 2;
mouseY = frame.getY() + frame.getHeight() / 2;
robot.mouseMove(mouseX, mouseY);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseWheel(10);
validate();
} finally {
frame.dispose();
}
}
private static void test(Robot robot) throws Exception {
SwingUtilities.invokeAndWait(() -> {
Point locationOnScreen = scrollPane.getLocationOnScreen();
point = new Point(
locationOnScreen.x + scrollPane.getWidth() / 2,
locationOnScreen.y + scrollPane.getHeight() / 2);
});
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
// vertical scroll bar is enabled
initScrollPane(true, false);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// vertical scroll bar is enabled + shift
initScrollPane(true, false);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, false);
// horizontal scroll bar is enabled
initScrollPane(false, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(false, true);
// horizontal scroll bar is enabled + shift
initScrollPane(false, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
// both scroll bars are enabled
initScrollPane(true, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// both scroll bars are enabled + shift
initScrollPane(true, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
}
private static void test(Robot robot) throws Exception {
SwingUtilities.invokeAndWait(() -> {
Point locationOnScreen = scrollPane.getLocationOnScreen();
point = new Point(
locationOnScreen.x + scrollPane.getWidth() / 2,
locationOnScreen.y + scrollPane.getHeight() / 2);
});
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
// vertical scroll bar is enabled
initScrollPane(true, false);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// vertical scroll bar is enabled + shift
initScrollPane(true, false);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, false);
// horizontal scroll bar is enabled
initScrollPane(false, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(false, true);
// horizontal scroll bar is enabled + shift
initScrollPane(false, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
// both scroll bars are enabled
initScrollPane(true, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// both scroll bars are enabled + shift
initScrollPane(true, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
}
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(50);
SwingUtilities.invokeAndWait(bug8033000::createAndShowGUI);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> {
Point locationOnScreen = scrollPane.getLocationOnScreen();
point = new Point(
locationOnScreen.x + scrollPane.getWidth() / 2,
locationOnScreen.y + scrollPane.getHeight() / 2);
});
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
// vertical scroll bar is enabled
initScrollPane(true, false);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true);
// vertical scroll bar is enabled + shift
initScrollPane(true, false);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(true);
// horizontal scroll bar is enabled
initScrollPane(false, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(false);
// horizontal scroll bar is enabled + shift
initScrollPane(false, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false);
// both scroll bars are enabled
initScrollPane(true, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true);
// both scroll bars are enabled + shift
initScrollPane(true, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false);
}
private static void test(Robot robot) throws Exception {
SwingUtilities.invokeAndWait(() -> {
Point locationOnScreen = scrollPane.getLocationOnScreen();
point = new Point(
locationOnScreen.x + scrollPane.getWidth() / 2,
locationOnScreen.y + scrollPane.getHeight() / 2);
});
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
// vertical scroll bar is enabled
initScrollPane(true, false);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// vertical scroll bar is enabled + shift
initScrollPane(true, false);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, false);
// horizontal scroll bar is enabled
initScrollPane(false, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(false, true);
// horizontal scroll bar is enabled + shift
initScrollPane(false, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
// both scroll bars are enabled
initScrollPane(true, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// both scroll bars are enabled + shift
initScrollPane(true, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
}
private static void test(Robot robot) throws Exception {
SwingUtilities.invokeAndWait(() -> {
Point locationOnScreen = scrollPane.getLocationOnScreen();
point = new Point(
locationOnScreen.x + scrollPane.getWidth() / 2,
locationOnScreen.y + scrollPane.getHeight() / 2);
});
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
// vertical scroll bar is enabled
initScrollPane(true, false);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// vertical scroll bar is enabled + shift
initScrollPane(true, false);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, false);
// horizontal scroll bar is enabled
initScrollPane(false, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(false, true);
// horizontal scroll bar is enabled + shift
initScrollPane(false, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
// both scroll bars are enabled
initScrollPane(true, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// both scroll bars are enabled + shift
initScrollPane(true, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
}
private static void test(Robot robot) throws Exception {
SwingUtilities.invokeAndWait(() -> {
Point locationOnScreen = scrollPane.getLocationOnScreen();
point = new Point(
locationOnScreen.x + scrollPane.getWidth() / 2,
locationOnScreen.y + scrollPane.getHeight() / 2);
});
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
// vertical scroll bar is enabled
initScrollPane(true, false);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// vertical scroll bar is enabled + shift
initScrollPane(true, false);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, false);
// horizontal scroll bar is enabled
initScrollPane(false, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(false, true);
// horizontal scroll bar is enabled + shift
initScrollPane(false, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
// both scroll bars are enabled
initScrollPane(true, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// both scroll bars are enabled + shift
initScrollPane(true, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
}
private static void test(Robot robot) throws Exception {
SwingUtilities.invokeAndWait(() -> {
Point locationOnScreen = scrollPane.getLocationOnScreen();
point = new Point(
locationOnScreen.x + scrollPane.getWidth() / 2,
locationOnScreen.y + scrollPane.getHeight() / 2);
});
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
// vertical scroll bar is enabled
initScrollPane(true, false);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// vertical scroll bar is enabled + shift
initScrollPane(true, false);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, false);
// horizontal scroll bar is enabled
initScrollPane(false, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(false, true);
// horizontal scroll bar is enabled + shift
initScrollPane(false, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
// both scroll bars are enabled
initScrollPane(true, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// both scroll bars are enabled + shift
initScrollPane(true, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
}
private static void test(Robot robot) throws Exception {
SwingUtilities.invokeAndWait(() -> {
Point locationOnScreen = scrollPane.getLocationOnScreen();
point = new Point(
locationOnScreen.x + scrollPane.getWidth() / 2,
locationOnScreen.y + scrollPane.getHeight() / 2);
});
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
// vertical scroll bar is enabled
initScrollPane(true, false);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// vertical scroll bar is enabled + shift
initScrollPane(true, false);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, false);
// horizontal scroll bar is enabled
initScrollPane(false, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(false, true);
// horizontal scroll bar is enabled + shift
initScrollPane(false, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
// both scroll bars are enabled
initScrollPane(true, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// both scroll bars are enabled + shift
initScrollPane(true, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
}
private static void test(Robot robot) throws Exception {
SwingUtilities.invokeAndWait(() -> {
Point locationOnScreen = scrollPane.getLocationOnScreen();
point = new Point(
locationOnScreen.x + scrollPane.getWidth() / 2,
locationOnScreen.y + scrollPane.getHeight() / 2);
});
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
// vertical scroll bar is enabled
initScrollPane(true, false);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// vertical scroll bar is enabled + shift
initScrollPane(true, false);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, false);
// horizontal scroll bar is enabled
initScrollPane(false, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(false, true);
// horizontal scroll bar is enabled + shift
initScrollPane(false, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
// both scroll bars are enabled
initScrollPane(true, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true, false);
// both scroll bars are enabled + shift
initScrollPane(true, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false, true);
}
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(50);
SwingUtilities.invokeAndWait(bug8033000::createAndShowGUI);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> {
Point locationOnScreen = scrollPane.getLocationOnScreen();
point = new Point(
locationOnScreen.x + scrollPane.getWidth() / 2,
locationOnScreen.y + scrollPane.getHeight() / 2);
});
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
// vertical scroll bar is enabled
initScrollPane(true, false);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true);
// vertical scroll bar is enabled + shift
initScrollPane(true, false);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(true);
// horizontal scroll bar is enabled
initScrollPane(false, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(false);
// horizontal scroll bar is enabled + shift
initScrollPane(false, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false);
// both scroll bars are enabled
initScrollPane(true, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true);
// both scroll bars are enabled + shift
initScrollPane(true, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false);
}
public static void main(String[] args) throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(50);
SwingUtilities.invokeAndWait(bug8033000::createAndShowGUI);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> {
Point locationOnScreen = scrollPane.getLocationOnScreen();
point = new Point(
locationOnScreen.x + scrollPane.getWidth() / 2,
locationOnScreen.y + scrollPane.getHeight() / 2);
});
robot.mouseMove(point.x, point.y);
robot.waitForIdle();
// vertical scroll bar is enabled
initScrollPane(true, false);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true);
// vertical scroll bar is enabled + shift
initScrollPane(true, false);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(true);
// horizontal scroll bar is enabled
initScrollPane(false, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(false);
// horizontal scroll bar is enabled + shift
initScrollPane(false, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false);
// both scroll bars are enabled
initScrollPane(true, true);
robot.waitForIdle();
robot.mouseWheel(delta);
robot.waitForIdle();
checkScrollPane(true);
// both scroll bars are enabled + shift
initScrollPane(true, true);
robot.waitForIdle();
robot.keyPress(KeyEvent.VK_SHIFT);
robot.mouseWheel(delta);
robot.keyRelease(KeyEvent.VK_SHIFT);
robot.waitForIdle();
checkScrollPane(false);
}