java 鼠标获取位置_获取鼠标位置

回答(10)

e15298c6a3b4591803e154ab0c3b3e2e.png

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);

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

在SWT中,您无需在听众中找到鼠标位置 . Display对象的方法为 getCursorLocation() .

在vanilla SWT / JFace中,调用 Display.getCurrent().getCursorLocation() .

在RCP应用程序中,调用 PlatformUI.getWorkbench().getDisplay().getCursorLocation() .

对于SWT应用程序,最好在其他人提到的 MouseInfo.getPointerInfo() 上使用 getCursorLocation() ,因为后者是在SWT旨在替换的AWT工具包中实现的 .

e15298c6a3b4591803e154ab0c3b3e2e.png

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 + ")");

}

}

}

e15298c6a3b4591803e154ab0c3b3e2e.png

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

}

}

}

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

尝试查看java.awt.Robot类 . 它允许您以编程方式移动鼠标 .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

如果您使用Swing作为UI图层,则可以使用Mouse-Motion Listener .

e15298c6a3b4591803e154ab0c3b3e2e.png

2 years ago

如果您正在使用SWT,则可能需要查看添加MouseMoveListener,如here所述 .

e15298c6a3b4591803e154ab0c3b3e2e.png

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;

}

e15298c6a3b4591803e154ab0c3b3e2e.png

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;

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值