回答(10)
2 years ago
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int x = (int) b.getX();
int y = (int) b.getY();
System.out.print(y + "jjjjjjjjj");
System.out.print(x);
Robot r = new Robot();
r.mouseMove(x, y - 50);
2 years ago
在SWT中,您无需在听众中找到鼠标位置 . Display对象的方法为 getCursorLocation() .
在vanilla SWT / JFace中,调用 Display.getCurrent().getCursorLocation() .
在RCP应用程序中,调用 PlatformUI.getWorkbench().getDisplay().getCursorLocation() .
对于SWT应用程序,最好在其他人提到的 MouseInfo.getPointerInfo() 上使用 getCursorLocation() ,因为后者是在SWT旨在替换的AWT工具包中实现的 .
2 years ago
import java.awt.MouseInfo;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.*;
public class MyClass {
public static void main(String[] args) throws InterruptedException{
while(true){
//Thread.sleep(100);
System.out.println("(" + MouseInfo.getPointerInfo().getLocation().x +
", " +
MouseInfo.getPointerInfo().getLocation().y + ")");
}
}
}
2 years ago
import java.awt.MouseInfo;
import java.util.concurrent.TimeUnit;
public class Cords {
public static void main(String[] args) throws InterruptedException {
//get cords of mouse code, outputs to console every 1/2 second
//make sure to import and include the "throws in the main method"
while(true == true)
{
TimeUnit.SECONDS.sleep(1/2);
double mouseX = MouseInfo.getPointerInfo().getLocation().getX();
double mouseY = MouseInfo.getPointerInfo().getLocation().getY();
System.out.println("X:" + mouseX);
System.out.println("Y:" + mouseY);
//make sure to import
}
}
}
2 years ago
尝试查看java.awt.Robot类 . 它允许您以编程方式移动鼠标 .
2 years ago
如果您使用Swing作为UI图层,则可以使用Mouse-Motion Listener .
2 years ago
如果您正在使用SWT,则可能需要查看添加MouseMoveListener,如here所述 .
2 years ago
在我的场景中,我应该根据用鼠标完成的GUI操作在鼠标位置打开一个对话框 . 以下代码对我有用:
public Object open() {
//create the contents of the dialog
createContents();
//setting the shell location based on the curent position
//of the mouse
PointerInfo a = MouseInfo.getPointerInfo();
Point pt = a.getLocation();
shellEO.setLocation (pt.x, pt.y);
//once the contents are created and location is set-
//open the dialog
shellEO.open();
shellEO.layout();
Display display = getParent().getDisplay();
while (!shellEO.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
return result;
}
2 years ago
我正在做这样的事情来使用机器人获得鼠标坐标,我在我正在开发的几个游戏中进一步使用这些坐标:
public class ForMouseOnly {
public static void main(String[] args) throws InterruptedException {
int x = MouseInfo.getPointerInfo().getLocation().x;
int y = MouseInfo.getPointerInfo().getLocation().y;
while (true) {
if (x != MouseInfo.getPointerInfo().getLocation().x || y != MouseInfo.getPointerInfo().getLocation().y) {
System.out.println("(" + MouseInfo.getPointerInfo().getLocation().x + ", "
+ MouseInfo.getPointerInfo().getLocation().y + ")");
x = MouseInfo.getPointerInfo().getLocation().x;
y = MouseInfo.getPointerInfo().getLocation().y;
}
}
}
}