java語言鼠標移動實現


更換博客發布地址:http://ihongqiqu.com

 

java語言鼠標移動的實現有很多方法,這里是一種簡單實用方法。

鼠標移動實現:鼠標移動是相對移動,即相對鼠標當前的位置而做的移動。這里我們需要獲取鼠標當前位置的坐標,可以通過java.awt.MouseInfo類來獲取鼠標信息,然后用java.awt.robot來實現鼠標位置的移動。

代碼見下:

 1 import java.awt.Dimension;
2 import java.awt.Robot;
3 import java.awt.Point;
4 import java.awt.Toolkit;
5 import java.awt.MouseInfo;
6 import java.awt.AWTException;
7 import java.awt.event.InputEvent;
8 import javax.swing.*;
9
10 public class MyMouseController{
11
12 private Dimension dim; //存儲屏幕尺寸
13 private Robot robot;//自動化對象
14
15 public MyMouseController(){
16 dim = Toolkit.getDefaultToolkit().getScreenSize();
17 System.out.println("屏幕大小為:" + dim.getWidth() + " " + dim.getHeight());
18 try{
19 robot = new Robot();
20 }catch(AWTException e){
21 e.printStackTrace();
22 }
23 }
24
25 public void Move(int width,int heigh){ //鼠標移動函數
26 System.out.println("enter Move()...");
27 Point mousepoint = MouseInfo.getPointerInfo().getLocation();
28 System.out.println("移動前坐標:" + mousepoint.x + " " + mousepoint.y);
29 width += mousepoint.x;
30 heigh += mousepoint.y;
31 try{
32 robot.delay(3000);
33 robot.mouseMove(width,heigh);
34 }catch(Exception e){
35 e.printStackTrace();
36 }
37 System.out.println("移動后坐標:" + width + " " + heigh);
38 //robot.mousePress(InputEvent.BUTTON1_MASK);//鼠標單擊
39 }
40
41
42 public static void main(String args[])throws Exception{
43
44
45
46 MyMouseController mmc = new MyMouseController();
47
48 System.out.println("mouse control start:");
49 mmc.Move(20,20);//坐標為相對坐標
50 System.out.println("=======第二次移動=======");
51 mmc.Move(512,384);
52
53 System.out.println("mouse control stop.");
54
55 }
56 }

本程序實現兩次移動 第一次移動相對坐標為(20,20)第二次為(512,384)。

更換博客發布地址:http://ihongqiqu.com

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM